The Setmobiledataenabled Method Is No Longer Callable as of Android L and Later

Android L: Data turn off error

setMobileDataEnabled has been removed in Android L.

Use this instead:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Method methodSet = Class.forName(tm.getClass().getName()).getDeclaredMethod( "setDataEnabled", Boolean.TYPE);
methodSet.invoke(tm,true);

Make sure you have this permission on your manifest:

<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>

This permission is for System apps only

iOS/Android - Disabling Mobile Data (Cellular Data) via SDK

The short answer is no as for android, it cannot be turned on/off automatically. User action is required.

The best you can do is to guide a user to the settings screen, where they can enable/disable it manually.

the setMobileDataEnabled() method is no longer callable via reflection. It was callable since Android 2.1 (API 7) to Android 4.4 (API 19) via reflection, but as of Android 5.0 and later, even with the rooted phones, the setMobileDataEnabled() method is not callable. You need to have MODIFY_PHONE_STATE permission which is only given to system or signature apps.

There are various solution provided for rooted device, which you can have a look here.



Related Topics



Leave a reply



Submit