简体   繁体   中英

How to put a phone in Airplane mode programmatically in Android?

谁能告诉我如何通过在Android中单击按钮以编程方式将手机置于飞行模式?

See the blog article http://dustinbreese.blogspot.in/2009/04/andoid-controlling-airplane-mode.html ,

Works only upto API 16

// Toggle airplane mode.
Settings.System.putInt(
      context.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);

where isEnabled is whether airplane mode is enabled or not.

try this to put your phone on airplane mode.

// 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);

Also make sure you have the WRITE_SETTINGS permission

 this code to make phone silent

        AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        am.setRingerMode(AudioManager.RINGER_MODE_SILENT);

there is also vibrate mode and normal mode

       am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
       am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM