0

Using android adb shell in API level 17 i can enable and disable mobile data using svc data enable/disable but if i tried to do it pragmatically it does work and pass with out any exception here is my code

    try{ Log.e(TAG,"Before mobile data thread");
    Process proc = Runtime.getRuntime().exec("su");
    DataOutputStream os= new DataOutputStream(proc.getOutputStream());
    os.writeBytes("svc data disable\n");
    os.writeBytes("exit\n");
    os.flush();
    try{
        proc.waitFor();
        if(proc.exitValue() !=255)
        {
            Log.e(TAG,"Disabling mobile data");
        }else{
            Log.e(TAG,"Error Disabling mobile data");

            }

        }catch(Exception e){}
} catch (Exception ex){Log.e(TAG,"IN mobile data exception exception");} 

any Suggestion how can i disable/enable mobile data

1 Answers1

1

You can not enable or disable mobile data yourself if phone is not rooted or your app is not in device administrator mode. You can only ask user to enable/disable application data.

You can ask user in dialog like this:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Please turn on mobile data access")
    .setTitle("Unable to connect")
    .setCancelable(false)
    .setPositiveButton("Settings", (dialog, id) -> startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)))
    .setNegativeButton("Cancel", null)
    .show()

For rooted phone use this answer

Community
  • 1
  • 1
IlyaGulya
  • 957
  • 6
  • 18