您的位置:首页 > 移动开发 > Android开发

setMobileDataEnabled方法不能再被调用 Android L and later

2016-07-26 22:57 302 查看
场景:

个人开发的省电APP,有这么一个功能,可以快速打开GPRS数据,关闭GPRS数据,这算是必须的功能,放在之前,很好解决,但是现在,安卓越来越考虑用户体验和安全性,那么给非系统APP的权限就越来越少了!

问题来了:

从 Android 2.1 < API 7 >到 Android 4.4 < API 19 >setMobileDataEnabled()可以通过反射方式被调用了, 但是到了Android L以后 ,即使你有root权限, setMobileDataEnabled() method 也不能被调用,捷径没有了!

可以自己打印一下 看这个方法 还有没有

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之前的判断移动数据是否打开的做法:

/**
* 反射获得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之后可以这样判断网络是否打开Settings.Global.getInt(context.getContentResolver(), “mobile_data”, 0),但是没有ROOT权限依然很难主动打开GPRS,宝宝心里苦啊

如果你是系统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设备上

(1=enable, 0=disable):

settings put global mobile_data 1

settings put global mobile_data 0

没权限,没权限,没权限,重要的事说三遍~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: