Turn mobile data on or off in Lollipop

      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();
            }

      

The upto kitkat mechanism above works correctly, but the lollipop fails, is there any other way to achieve this.

+3


source to share


1 answer


SetMobileDataEnabled was a private API that was used by developers via reflection as described in your code. Now the google team has completely removed this API without warning, so after kitkat there is no other API available to switch network state. But if you have an available phone, you can try the second answer from nickkadrov here.

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

      



But if your phone is not rooted, sorry, there is currently no API or method available to switch network state.

+3


source







All Articles