toggle Flight Mode - Android Phone

Android examples for Phone:Airplane Mode

Description

toggle Flight Mode

Demo Code


import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

public class Main{
    /**toggle Flight Mode*/
    public static void toggle(Context context) throws Exception {
        if (Build.VERSION.SDK_INT >= 17) {
            toggleAboveApiLevel17();//from ww w.ja  va 2s .c  o m
        } else {
            toggleBelowApiLevel17(context);
        }
    }
    private static void toggleAboveApiLevel17() throws Exception {
        // Android 4.2 and above
        try {
            ShellUtils.sudo("ndc", "resolver", "flushdefaultif");
        } catch (Exception e) {
            ShellUtils.sudo("settings", "put", "global",
                    "airplane_mode_on", "1");
            ShellUtils.sudo("am", "broadcast", "-a",
                    "android.intent.action.AIRPLANE_MODE", "--ez state",
                    "true");
            ShellUtils.sudo("settings", "put", "global",
                    "airplane_mode_on", "0");
            ShellUtils.sudo("am", "broadcast", "-a",
                    "android.intent.action.AIRPLANE_MODE", "--ez state",
                    "false");
        }
    }
    @SuppressWarnings("deprecation")
    private static void toggleBelowApiLevel17(Context context)
            throws Exception {
        // Android 4.2 below
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", true);
        context.sendBroadcast(intent);
        Thread.sleep(3000);
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0);
        intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", false);
        context.sendBroadcast(intent);
    }
}

Related Tutorials