简体   繁体   English

在Android 6.0上切换飞行模式

[英]Toggling Airplane mode on Android 6.0

I'm currently trying to toggle the Airplane mode programmatically using Android version 6.0. 我目前正在尝试使用Android 6.0版以编程方式切换飞行模式。

I've done quite a lot research and some people say it's not possible anymore since Version 4.2, while others say there still are some workarounds, which I've tried but didn't work for me. 我已经做了很多研究,有人说从4.2版开始就不可能了,而另一些人说仍然有一些变通办法,我已经尝试过了,但对我却不起作用。

What I've tried last is the following: 我最后尝试的是以下内容:

public static boolean isAirplaneModeOn(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.System.getInt(context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0) != 0;
        } else {
            return Settings.Global.getInt(context.getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
        }
    }
    public static void setSettings(Context context, int value) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Settings.System.putInt(
                    context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, value);
        } else {
            Settings.Global.putInt(
                    context.getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, value);
        }
    }

But here I get the error Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS I know that I can change these settings on the emulator, but is it also possible to change these settings on my external device? 但是在这里我得到了错误“ Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS我知道我可以在模拟器上更改这些设置,但是也可以在外部设备上更改这些设置吗? In another thread some people said that for some devices it is possible, but I couldn't figure out any specific information about my Device (HUAWEI P8 Light). 在另一个话题中,有人说对于某些设备是可行的,但我无法弄清楚有关我的设备(HUAWEI P8 Light)的任何特定信息。 So my question is, is it possible to do so, or is there even a better way to do it? 所以我的问题是,有可能这样做,还是有更好的方法呢? I've also read some about using ADB to toggle airplane mode, but there they said that this would need the same permissions. 我也阅读了一些有关使用ADB切换飞行模式的信息,但是他们说这将需要相同的权限。 I hope somebody can help me out. 我希望有人能帮助我。 :) :)

this is not available for non system applications anymore , because Google denied this on API level. 此功能已不再适用于非系统应用程序,因为Google在API级别上对此予以拒绝。 It's possible for data connection to be enabled/disabled on rooted phones tho 可以在有根电话上启用/禁用数据连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android模拟器中以“飞机”模式进行测试 - Testing in “Airplane” mode in Android Emulator 如何在 Android 上检测飞行模式? - How can one detect airplane mode on Android? 使用MQTT Android服务从飞行模式重新连接 - Reconnecting from Airplane Mode with MQTT Android Service Android 关闭飞行模式时应用程序崩溃 - Android App crushes when airplane mode is turned off 打开和关闭飞行模式以及如何在android中打开移动数据 - turning airplane mode on and off and how to turn on mobile data in android 如何在 Doze 模式下将应用列入白名单 Android 6.0 - How to WhiteList app in Doze mode Android 6.0 如何更改Android的飞行模式的行为,以便它不会关闭蜂窝无线电? - How can I change the behavior of Android's airplane mode so that it will not turn off the cellular radio? 关闭飞行模式时,为什么 Android 应用程序 go 通过活动和片段生命周期方法 - Why does an Android app go through the activity and fragment lifecycle methods when airplane mode is turned off 切换指定网络的飞行模式。 - Toggle on/off the airplane mode for the given network. 设置plane_mode_on已从android.provider.Settings.System移至android.provider.Settings.Global,返回只读值 - Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM