Enable or Disable Mobile Data in Lollipop

851 views Asked by At
      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 above mechanism is working properly but in lollipop it is not worked out is there any other way to achieve this.

1

There are 1 answers

2
Adi Tiwari On

SetMobileDataEnabled was a private API which was being used by developers through reflection as described in your code. Now the Google team has compleatly removed that API without warning so after kitkat there is no any other API available for toggling network state. But if you have a rooted phone available you can try the second answer by 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 non-rooted, sorry currently no API or method available for toggling network state.