android - 在 Android 中关闭飞行模式

标签 android mode airplane-mode

如果 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/

相关文章:

ios - 是否有一种(合法的)方法可以在 iOS 下捕获整个屏幕?

android - Gridview 元素对齐

java - 类型 new MediaPlayer.OnCompletionListener(){} 必须实现继承的抽象方法

networking - Kafka Docker network_mode

winapi - 如何在 Windows 8 中以编程方式检查/切换飞行模式?

ios - iOS 应用程序能否以编程方式将 Apple Watch 置于飞行模式?

android - 使用 Room Library 插入 JSON 数据

android - 通用图像加载器给出错误图像无法解码android

python - GroupBy pandas DataFrame 并选择最常见的值

c++ - 在 C++ 中查找整数 vector 的模式