我一直用这个code以编程方式启用移动数据:
I've always used this code to enable mobile data programmatically:
ConnectivityManager conman = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
@SuppressWarnings("rawtypes")
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
@SuppressWarnings("rawtypes")
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
@SuppressWarnings("unchecked")
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, true);
这效果不错,除了现在在Android 4.4.2,我得到这个异常:
This worked well, except now on Android 4.4.2 where I get this exception:
java.lang.NoSuchMethodException: setMobileDataEnabled [boolean]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getDeclaredMethod(Class.java:640)
at com.test.auto3gPro.ClasseConnessione.settaConnessione(ClasseConnessione.java:48)
at com.test.auto3gPro.receiver.ScreenBroadcastReceiver.onReceive(ScreenBroadcastReceiver.java:108)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5081)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
有谁知道如何解决这个问题?
Does anyone know how to fix this?
如果您正在使用的CyanogenMod setMobileDataEnabled方法(布尔)在setMobileDataEnabled(字符串,布尔)改变......你可以看到在这条线上code 的。
If you are using cyanogenmod the method setMobileDataEnabled(boolean) is changed in setMobileDataEnabled(String, boolean)...as you can see on this line of code.
所以,你可以用标准的方式,然后在NoSuchMethodException catch块尝试的CyanogenMod的方式是这样的:
So you can use the standard way and then in the NoSuchMethodException catch block try the "cyanogenmod" way like this:
Class[] cArg = new Class[2];
cArg[0] = String.class;
cArg[1] = Boolean.TYPE;
Method setMobileDataEnabledMethod;
setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", cArg);
Object[] pArg = new Object[2];
pArg[0] = getContext().getPackageName();
pArg[1] = true;
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, pArg);
我不知道其他器官功能障碍综合征的影响。
I don't know if other mods are affected.
上一篇:认识多(互操作性).NET解决方案的配置文件操作性、配置文件、解决方案、NET
下一篇:PhoneGap的PushNotification打开特定的应用程序页面应用程序、页面、PhoneGap、PushNotification