open Flight Mode - Android Phone

Android examples for Phone:Airplane Mode

Description

open 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{
    /**open Flight Mode*/
    public static void openFlight(Context context) throws Exception {
        if (Build.VERSION.SDK_INT >= 17) {
            openFlightAboveApiLevel17();
        } else {// w  ww.  j  a  v a2  s.  c  o  m
            openBelowApiLevel17(context);
        }
    }
    private static void openFlightAboveApiLevel17() throws Exception {
        try {
            ShellUtils.sudo("settings", "put", "global",
                    "airplane_mode_on", "1");
            ShellUtils.sudo("am", "broadcast", "-a",
                    "android.intent.action.AIRPLANE_MODE", "--ez state",
                    "true");
            Log.e("xmg", "sudo success");
        } catch (Exception e) {
            Log.e("xmg", "sudo fail");
            ShellUtils.sudo("ndc", "resolver", "flushdefaultif");
        }
    }
    @SuppressWarnings("deprecation")
    private static void openBelowApiLevel17(Context context)
            throws Exception {
        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);
    }
}

Related Tutorials