简体   繁体   English

如何在Android中以编程方式将手机置于飞行模式?

[英]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 , 请参阅博客文章http://dustinbreese.blogspot.in/2009/04/andoid-controlling-airplane-mode.html

Works only upto API 16 仅适用于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. isEnabled是其中是否启用飞行模式。

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 另外,请确保您具有WRITE_SETTINGS权限

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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