場景:
個人開發(fā)的省電APP,有這么一個功能,可以快速打開GPRS數(shù)據(jù),關(guān)閉GPRS數(shù)據(jù),這算是必須的功能,放在之前,很好解決,但是現(xiàn)在,安卓越來越考慮用戶體驗和安全性,那么給非系統(tǒng)APP的權(quán)限就越來越少了!
問題來了:
從 Android 2.1 < API 7 >到 Android 4.4 < API 19 >setMobileDataEnabled()可以通過反射方式被調(diào)用了, 但是到了Android L以后 ,即使你有root權(quán)限, setMobileDataEnabled() method 也不能被調(diào)用,捷徑?jīng)]有了! 可以自己打印一下 看這個方法 還有沒有
final Class<?> conmanClass = Class.forName(context.getSystemService(Context.CONNECTIVITY_SERVICE).getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(context.getSystemService(Context.CONNECTIVITY_SERVICE));
final Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method[] methods = iConnectivityManagerClass.getDeclaredMethods();
for (final Method method : methods) {
if (method.toGenericString().contains("set")) {
Log.i("TESTING", "Method: " + method.getName());
}
}
安卓5.0 LOLLIPOP之前的判斷移動數(shù)據(jù)是否打開的做法:
/**
* 反射獲得IConnectivityManager實例
*
* @param context
* @return
*/
private Object getConnectivityManager(Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?> conMgrClass = null;
Object iConMgr = null;
try {
conMgrClass = Class.forName(conMgr.getClass().getName());//ConnectivityManager
Field iConMgrField = conMgrClass.getDeclaredField("mService");//IConnectivityManager
iConMgrField.setAccessible(true);
iConMgr = iConMgrField.get(conMgr);//獲得ConnectivityManager的IConnectivityManager實例
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return iConMgr;
}
private boolean isMobileEnabled(Context context) {
try {
Object iConMgr = getConnectivityManager(context);//IConnectivityManager
Class<?> iConMgrClass = Class.forName(iConMgr.getClass().getName());
Method getMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("getMobileDataEnabled");
getMobileDataEnabledMethod.setAccessible(true);
return (Boolean) getMobileDataEnabledMethod.invoke(getConnectivityManager(context));
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private void setValue(Context context, boolean isopen) {
try {
Object iConMgr = getConnectivityManager(context);//IConnectivityManager
Class<?> iConMgrClass = Class.forName(iConMgr.getClass().getName());
Method setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.class);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConMgr, isopen);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 判斷GPRS是否打開
*
* @return
*/
public boolean isOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return isMobileDataEnabledFromLollipop(mContext);
}
return isMobileEnabled(mContext);
}
private boolean isMobileDataEnabledFromLollipop(Context context) {
boolean state = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
state = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0) == 1;
}
return state;
}
5.0之后可以這樣判斷網(wǎng)絡(luò)是否打開Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0),但是沒有ROOT權(quán)限依然很難主動打開GPRS,寶寶心里苦啊
如果你是系統(tǒng)APP,那么好辦了這里我查了下,有2種方法
第一種:
public void setMobileDataState(boolean mobileDataEnabled) {
try {
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
if (null != setMobileDataEnabledMethod) {
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
}
} catch (Exception ex) {
Log.e(TAG, "Error setting mobile data state", ex);
}
}
public boolean getMobileDataState() {
try {
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getMobileDataEnabledMethod) {
boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
return mobileDataEnabled;
}
} catch (Exception ex) {
Log.e(TAG, "Error getting mobile data state", ex);
}
return false;
}
IConnectivityManager的原方法被搬到 TelephonyManager , 多虧了getDataEnabled和setDataEnabled這左膀右臂~~~,又可以愉快的玩耍了
第二種:
涉及Settings.Global類的mobile_data屬性Settings.Global.getInt(contentResolver, "mobile_data");
為了使能 mobile data 咱們可以用 shell commands 在ROOT設(shè)備上
(1=enable, 0=disable)
:settings put global mobile_data 1
settings put global mobile_data 0
沒權(quán)限,沒權(quán)限,沒權(quán)限,重要的事說三遍~~~