在Lollipop中启用或禁用移动数据

时间:2015-06-22 12:57:44

标签: android reflection mobile-development

      try {
             ConnectivityManager connectivityManager = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
               Method iMthd = ConnectivityManager.class.getDeclaredMethod(
                        "setMobileDataEnabled", boolean.class);
                if (iMthd != null) {
                    iMthd.setAccessible(false);
                    iMthd.invoke(connectivityManager, enable);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

upto kitkat上面的机制工作正常,但在棒棒糖中没有解决是否有任何其他方法来实现这一点。

1 个答案:

答案 0 :(得分:3)

SetMobileDataEnabled是一个私有API,开发人员通过代码中描述的反射使用它。现在,Google团队已经完全删除了该API而没有任何警告,因此在kitkat之后没有任何其他API可用于切换网络状态。但是如果你有一个有根电话,你可以试试nickkadrov在这里的第二个答案

http://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later

但是如果您的手机没有root权限,抱歉目前没有可用于切换网络状态的API或方法。

相关问题