以编程方式在所有API级别上启用飞行模式

时间:2014-05-08 09:11:08

标签: android

如何在点击按钮时以编程方式启用/禁用飞行模式? 我试过这个

// read the airplane mode setting
  boolean isEnabled = Settings.System.getInt(
  getContentResolver(), 
  Settings.System.AIRPLANE_MODE_ON, 0) == 1;

// toggle airplane mode
Settings.System.putInt(
  getContentResolver(),
  Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

// Post an intent to reload
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);

但我收到错误“系统无法解析或不是字段”,因为它需要API 16或更高版本

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

有没有这样做的工作方式适用于所有api级别?

2 个答案:

答案 0 :(得分:3)

public class MainActivity extends AppCompatActivity {
private static final String TAG = "AirplaneModeActivity";
private final String COMMAND_FLIGHT_MODE_1 = "settings put global airplane_mode_on";
private final String COMMAND_FLIGHT_MODE_2 = "am broadcast -a android.intent.action.AIRPLANE_MODE --ez state";
ToggleButton toggleBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toggleBtn = (ToggleButton) findViewById(R.id.toggle_btn);
    toggleBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setFlightMode(MainActivity.this);


        }

        public void setFlightMode(Context context) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                // API 17 onwards.
                if (isRooted(context)) {
                    int enabled = isFlightModeEnabled(context) ? 0 : 1;
                    // Set Airplane / Flight mode using su commands.
                    String command = COMMAND_FLIGHT_MODE_1 + " " + enabled;
                    executeCommandWithoutWait(context, "-c", command);
                    command = COMMAND_FLIGHT_MODE_2 + " " + enabled;
                    executeCommandWithoutWait(context, "-c", command);
                } else {
                    try {
                        // No root permission, just show Airplane / Flight mode setting screen.
                        Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    } catch (ActivityNotFoundException e) {
                        Log.e(TAG, "Setting screen not found due to: " + e.fillInStackTrace());
                    }
                }
            } else {
                // API 16 and earlier.
                boolean enabled = isFlightModeEnabled(context);
                Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, enabled ? 0 : 1);
                Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
                intent.putExtra("state", !enabled);
                sendBroadcast(intent);
            }
        }

        private  boolean isRooted(Context context) {

            // get from build info
            String buildTags = android.os.Build.TAGS;
            if (buildTags != null && buildTags.contains("test-keys")) {
                return true;
            }

            // check if /system/app/Superuser.apk is present
            try {
                File file = new File("/system/app/Superuser.apk");
                if (file.exists()) {
                    return true;
                }
            } catch (Exception e1) {
                // ignore
            }

            // try executing commands
            return canExecuteCommand("/system/xbin/which su")
                    || canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su");
        }

        // executes a command on the system
        private  boolean canExecuteCommand(String command) {
            boolean executedSuccesfully;
            try {
                Runtime.getRuntime().exec(command);
                executedSuccesfully = true;
            } catch (Exception e) {
                executedSuccesfully = false;
            }

            return executedSuccesfully;
        }



        @SuppressLint("NewApi")
        @SuppressWarnings("deprecation")
        private boolean isFlightModeEnabled(Context context) {
            boolean mode = false;
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                // API 17 onwards
                mode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
            } else {
                // API 16 and earlier.
                mode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
            }
            return mode;
        }

        private void executeCommandWithoutWait(Context context, String option, String command) {
            boolean success = false;
            String su = "su";
            for (int i = 0; i < 3; i++) {
                // "su" command executed successfully.
                if (success) {
                    // Stop executing alternative su commands below.
                    break;
                }
                if (i == 1) {
                    su = "/system/xbin/su";
                } else if (i == 2) {
                    su = "/system/bin/su";
                }
                try {
                    // execute command
                    Runtime.getRuntime().exec(new String[]{su, option, command});
                } catch (IOException e) {
                    Log.e(TAG, "su command has failed due to: " + e.fillInStackTrace());
                }
            }

            //boolean isEnabled = Settings.System.getInt(this.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) == 1;
               Settings.Global.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, setFlightMode(context); ? 0 : 1);


        }
    });

答案 1 :(得分:0)

主要

 package com.example.tutorial;
 import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.provider.Settings;
 import android.provider.Settings.Global;
 import android.view.View;
 import android.widget.Toast;

 public class MainActivity extends Activity {

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

 @SuppressLint("InlinedApi")
 private static boolean isAirplaneModeOn(Context context) {

 return Settings.System.getInt(context.getContentResolver(),
 Global.AIRPLANE_MODE_ON, 0) != 0;

 }

 @SuppressLint("NewApi")
 public void airplaneModeOn(View view) {
 try {

 android.provider.Settings.System.putInt(getContentResolver(),
 Global.AIRPLANE_MODE_ON,
 isAirplaneModeOn(getApplicationContext()) ? 0 : 1);

 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
 intent.putExtra("state", !isAirplaneModeOn(getApplicationContext()));
 sendBroadcast(intent);
 } catch (Exception e) {
 Toast.makeText(this, "Exception occured during Airplane Mode ON", Toast.LENGTH_LONG)
 .show();
 }
 }
 }

<强> main.xml中

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Toggle"
        android:onClick="airplaneModeOn" />

</RelativeLayout>

<强>的Manifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>