3
      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 以上机制工作正常,但在棒棒糖中没有解决是否有任何其他方法可以实现这一点。

4

1 回答 1

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 或方法。

于 2015-06-22T13:20:26.803 回答