在Android的切换飞行模式 [英] Toggle airplane mode in Android
本文介绍了在Android的切换飞行模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是不是犯了一个错误?它不工作。
公共无效飞机(){
布尔的IsEnabled = Settings.System.getInt(this.getApplicationContext()getContentResolver(),Settings.System.AIRPLANE_MODE_ON,0。)== 1;
Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,IsEnabled的?0:1);
//Settings.System.putInt(this.getApplicationContext().getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0:1);
意向意图=新的意图(Intent.ACTION_AIRPLANE_MODE_CHANGED);
}
解决方案
的这个答案包含code要做到这一点。另外,请确保您有 WRITE_SETTINGS
许可。
从控制飞行模式:
//读取飞行模式设置
布尔的IsEnabled = Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,0)== 1;
//切换飞行模式
Settings.System.putInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,IsEnabled的? 0:1);
//邮政意图重新加载
意向意图=新的意图(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra(国家,IsEnabled的!);
sendBroadcast(意向);
Did I make a mistake? It's not working.
public void airplane() {
boolean isEnabled = Settings.System.getInt(this.getApplicationContext().getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0 : 1);
//Settings.System.putInt(this.getApplicationContext().getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0 : 1);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
}
解决方案
This answer contains code necessary to do this. Also make sure you have the WRITE_SETTINGS
permission.
Adapted from Controlling 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);
这篇关于在Android的切换飞行模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文