Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latest update on enabling and disabling mobile data programmatically [duplicate]

People also ask

What is enable and disable mobile data in android programmatically?

For your find Information, unless you have a rooted phone I don't think you can enable and disable data programmatically because in order to do so we have to include MODIFY_PHONE_STATE permission which is only given to system or signature apps. setMobileDataEnabled() method is no longer callable via reflection.

Can mobile data be automatically turns on Android?

You can set your mobile phone to use mobile data automatically when the connection to the Wi-Fi network is weak. Before you can turn automatic use of mobile data on or off, you need to turn on mobile data. Slide your finger downwards starting from the top of the screen. Tap the settings icon.

How do I stop my mobile data from automatically turning on Android?

How do I enable or disable mobile data? The exact steps varies with Android version. You could enable / disable mobile data under Settings > SIM management, under Settings > Wireless & networks > Mobile networks or under Settings > Data usage .


It wont work on non-rooted phone as they added MODIFY_PHONE_STATE permission check. This permission is only given to system or signature apps refer-here.

Check the code below from PhoneInterfaceManager:

   @Override
   public void setDataEnabled(boolean enable) {
       enforceModifyPermission();
       mPhone.setDataEnabled(enable);
   }
   private void enforceModifyPermission() {
       mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
   }

Unless you have a rooted phone I don't think you can enable and disable data programmatically because in order to do so we have to include MODIFY_PHONE_STATE permission which is only given to system or signature apps.

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.


Fast forward to the end of 2018...

The short answer is it is no longer allowed to enable/disable mobile data programmatically. Here is the solution that I use all the time.

This code opens system data usage settings screen, where the user can disable mobile data manually.

public void enableDisableMobileData() {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName(
        "com.android.settings", 
        "com.android.settings.Settings$DataUsageSummaryActivity"));
    startActivity(intent);
}

EDIT 2019:

The answer above does not work starting on API 28. This is what works:

Intent intent = new Intent(Settings.ACTION_DATA_USAGE_SETTINGS);
startActivity(intent);

None of the above worked for me, but if your device is rooted, you may use this for enabling it.

private void enableMobileData(){

        try {
            String[] cmds = {"svc data enable"};
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            for (String tmpCmd : cmds) {
                os.writeBytes(tmpCmd + "\n");
            }
            os.writeBytes("exit\n");
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

}

And this for disabling it:

private void disableMobileData(){

    try {
        String[] cmds = {"svc data disable"};
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }

}