关闭飞行模式的Android [英] turn off airplane mode in Android
问题描述
我想关闭飞行模式如果num> 50,我实现了这个code(从的 -Android>切换飞行模式),但在执行时,我得到一个强制关闭,任何一个可以帮助吗?
I would like to turn off the airplane mode if num>50, I implemented this code (from Toggle airplane mode in Android) but when executed I get a force close, can any one help here?
if(num>50){
// 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);
}
o.k。我实现了premonitions但我想改变if语句:
o.k. I implemented the premonitions but i would like to change the if statement:
if num>=50 and airplane mode=on toggle it off
if airplane mode=off and num<50 toggle it on
能有人帮我写新的code? (我是一个新手)
Can some one help me writing the new code? (I'm a newbie)
推荐答案
您最有可能没有添加 WRITE_SETTING
权限您的的AndroidManifest.xml
:
You most likely did not add WRITE_SETTING
permissions to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
另外请注意,code:
Also note that code:
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
不应该工作,因为根据对<一个文档href=\"http://developer.android.com/reference/android/content/Intent.html#ACTION_AIRPLANE_MODE_CHANGED\"><$c$c>ACTION_AIRPLANE_MODE_CHANGED$c$c>:
这是一个受保护的意图只能由系统发送。
This is a protected intent that can only be sent by the system.
而且即使你现在可以把这个广播没有系统权限,它可能会在Android的未来版本的变化。
And even though you can currently send this broadcast without System permissions, it may change in future releases of Android.
这篇关于关闭飞行模式的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!