以编程方式启用飞行模式不会禁用移动数据和蓝牙

发布于 2025-01-19 13:20:16 字数 700 浏览 0 评论 0原文

我想通过代码打开飞行模式并关闭蓝牙、wifi和移动数据。 首先,在控制台中执行此操作:

adb shell pm grant com.package.name android.permission.WRITE_SECURE_SETTINGS

其次,从代码中打开飞行模式:

//Set radios cell,wifi,bluetooth witch will be off
Settings.Global.putString(requireContext().contentResolver,
   Settings.Global.AIRPLANE_MODE_RADIOS,
   "${Settings.Global.RADIO_CELL},
   ${Settings.Global.RADIO_WIFI},
   ${Settings.Global.RADIO_BLUETOOTH}"
)

Settings.Global.putInt(
    requireContext().contentResolver,
    Settings.Global.AIRPLANE_MODE_ON,  1
)

但在设置中,我看到只有 wifi 被关闭,蓝牙和移动日期仍然打开。为什么我不能通过代码将它们全部关闭?

I want to turn on airplane mode through the code and turn off bluetooth, wifi and mobile data.
First, execute this in the console:

adb shell pm grant com.package.name android.permission.WRITE_SECURE_SETTINGS

Second, turn on airplane mode from code:

//Set radios cell,wifi,bluetooth witch will be off
Settings.Global.putString(requireContext().contentResolver,
   Settings.Global.AIRPLANE_MODE_RADIOS,
   "${Settings.Global.RADIO_CELL},
   ${Settings.Global.RADIO_WIFI},
   ${Settings.Global.RADIO_BLUETOOTH}"
)

Settings.Global.putInt(
    requireContext().contentResolver,
    Settings.Global.AIRPLANE_MODE_ON,  1
)

But in the settings, I see that only wifi is turned off, bluetooth and mobile date are still on. Why can't I turn them all off from the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清眉祭 2025-01-26 13:20:16

首先,一些 Android 制造商需要使用 Settings.Secure 而不是 Settings.Global。通过命令检查

adb shell settings list global

其次,您应该尝试通知系统您的更改。

adb shell am broadcast -a android.intent.action.AIRPLANE_MODE

此意图有一个参数,几乎不需要在每个设备上使用来通知特定的飞机状态(1 - 打开,0 - 关闭):

adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state <state> 

First of all, some android manifactures require usage of Settings.Secure instead of Settings.Global. Check it via command

adb shell settings list global

Second of all, you should try to notify system about your changes.

adb shell am broadcast -a android.intent.action.AIRPLANE_MODE

There is parameter for this intent that is unneccessary to use on almost every device to notify specific airplane state (1 - ON, 0 - OFF):

adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state <state> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文