关闭 Android 中的飞行模式
如果 num>50 我想关闭飞行模式,我实现了此代码(来自 切换飞机Android 模式),但是执行时我强制关闭,有人可以帮忙吗?
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);
}
好的,我实现了预感,但我想更改 if 语句:
if num>=50 and airplane mode=on toggle it off
if airplane mode=off and num<50 toggle it on
有人可以帮助我编写新代码吗? (我是新手)
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. 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
Can some one help me writing the new code? (I'm a newbie)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您很可能没有向
AndroidManifest.xml
添加WRITE_SETTING
权限:另请注意该代码:
不应该工作,因为根据
ACTION_AIRPLANE_MODE_CHANGED
:尽管您目前可以在没有系统权限的情况下发送此广播,但它可能会在 Android 的未来版本中发生变化。
You most likely did not add
WRITE_SETTING
permissions to yourAndroidManifest.xml
:Also note that code:
Is not supposed to work, because according to documentation on
ACTION_AIRPLANE_MODE_CHANGED
:And even though you can currently send this broadcast without System permissions, it may change in future releases of Android.
确保在 Android 清单中设置了切换飞行模式的权限。
请查看此处在 Android 中切换飞行模式
Make sure to have set permissions for toggling airplane mode in your android manifest.
Take a look here Toggle airplane mode in Android
参考如下代码:
//
//
Refer following code:
//
//