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
How to Install Google Frameworks (Play, Accounts, etc.) on a Genymotion Virtual Device
Proper Implementation of Viewpager2 in Android
How to Draw Interactive Polyline on Route Google Maps V2 Android
Android: Create Spinner Programmatically from Array
How to Make Bitmap Compress Without Change the Bitmap Size
How to Use Custom Font in a Project Written in Android Studio
Solution to Install_Failed_Insufficient_Storage Error on Android
Set Transparent Background of an Imageview on Android
Android: Upgrading Db Version and Adding New Table
Difference Between Oncreate() and Onstart()
Receive Result from Dialogfragment
How to View the SQLite Database on an Android Device
How to Encrypt and Decrypt File in Android
Android: How to Display Video Thumbnails