-1

I have a rooting phone(5.0.1)
I want to switch on & off airplane mode every 40 seconds

not working

Settings.System.putInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 1);

then I tried:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String r1 = run_cmd("adb shell settings put global airplane_mode_on 1");
        String r3 = run_cmd("adb shell am broadcast -a android.intent.action.AIRPLANE_MODE");

        Log.e("test1", r1);
        Log.e("test3", r3);
    }

    private String run_cmd(String command) {
        StringBuilder output = new StringBuilder();

        java.lang.Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();
    }
}

how airplane mode change?

It looks like your post is mostly code; please add some more details.

  • Does this answer your question? [Toggle airplane mode in Android](https://stackoverflow.com/questions/5533881/toggle-airplane-mode-in-android) – Phung Duy Phong Jan 10 '20 at 08:23
  • Yes, Yes but.. I tried this code, but it didn't work. –  Jan 10 '20 at 08:43

1 Answers1

1

cmd code

public static final boolean execute(String cmd) {
        try {
            if (cmd != null && cmd.length() > 0) {
                Process p = Runtime.getRuntime().exec("su");
                DataOutputStream dos = new DataOutputStream(p.getOutputStream());
                dos.writeBytes(cmd + "\n");
                dos.writeBytes("exit\n");
                dos.flush();
                dos.close();
                p.waitFor();
            } else {
                Log.e(TAG, "command is null or empty");
            }
        } catch (IOException ex) {
            Log.e(TAG, "IOException");
            ex.printStackTrace();
        } catch (SecurityException ex) {
            Log.e(TAG, "SecurityException");
            ex.printStackTrace();
        } catch (Exception ex) {
            Log.e(TAG, "Generic Exception");
            ex.printStackTrace();
        }
        return false;
    }

main

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int second = 50;

        new CountDownTimer(second * 1000, second * 1000) {
            public void onTick(long l) {
            }

            public void onFinish() {
                RootPrivileges.execute("settings put global airplane_mode_on 1;" +
                        "am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true;");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                RootPrivileges.execute("settings put global airplane_mode_on 0;" +
                        "am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false;");
                start();
            }
        }.start();
    }
}

simple code - Resolved

  • I did a working implementation that also log error in the console https://github.com/bormat/ip_looper/blob/317175b3cd4c85a78b9300bfb1934cfd8fd7def7/app/src/main/java/com/example/iplooper/FirstFragment.java#L106 – bormat Mar 31 '21 at 22:17