如果 num>50,我想关闭飞行模式,我实现了这段代码(来自 Toggle airplane mode in 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
有人可以帮我写新代码吗? (我是新手)
最佳答案
您很可能没有向您的 AndroidManifest.xml
添加 WRITE_SETTING
权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
另请注意代码:
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
不应该工作,因为根据 ACTION_AIRPLANE_MODE_CHANGED
上的文档:
This is a protected intent that can only be sent by the system.
即使您目前可以在没有系统权限的情况下发送此广播,但它可能会在未来的 Android 版本中发生变化。
关于android - 在 Android 中关闭飞行模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7066427/