How to programmatically enable and disable Flight mode on Android 4.2?

眉间皱痕 提交于 2019-11-26 07:30:57

问题


Is there a way to disable or enable Flight Mode on Android 4.2?

I use this code that works only for previous Android versions:

android.provider.Settings.System.putInt(
    c.getContentResolver(),   
    android.provider.Settings.System.AIRPLANE_MODE_ON, enable ? 0 : 1
);

回答1:


There exist a simple workaround on rooted devices.

To enable Airplane Mode the following root shell commands can be used:

settings put global airplane_mode_on 1
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true

To disable Airplane Mode these root shell commands can be used:

settings put global airplane_mode_on 0
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false

Info taken from here

Disclaimer: This info is provided "as-is" without any form of warranty.




回答2:


This is no longer possible, except by apps that are signed by the firmware signing key or are installed on the system partition (typically by a rooted device user).




回答3:


You can't, as written in Official Android 4.2 API Documentation

Some device settings defined by Settings.System are now read-only. If your app attempts to write changes to settings defined in Settings.System that have moved to Settings.Global, the write operation will silently fail when running on Android 4.2 and higher. Even if your value for android:targetSdkVersion and android:minSdkVersion is lower than 17, your app is not able to modify the settings that have moved to Settings.Global when running on Android 4.2 and higher.

However, if you are the OS Developer, you can write it when you set these permissions

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

Then to write and read,

// To Write
Settings.Global.putString(getContentResolver(), "airplane_mode_on", "1");

// To Read
String result = Settings.Global.getString(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON);
Toast.makeText(this, "result:"+result, Toast.LENGTH_SHORT).show();



回答4:


For KitKat, you have to add android:sharedUserId="android.uid.system" in manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.airplanesetting"
    android:versionCode="1"
    android:versionName="1.0"
    android:sharedUserId="android.uid.system" >


来源:https://stackoverflow.com/questions/13766909/how-to-programmatically-enable-and-disable-flight-mode-on-android-4-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!