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

后端 未结 4 638
轻奢々
轻奢々 2020-11-27 04:28

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.Setti         


        
4条回答
  •  死守一世寂寞
    2020-11-27 04:44

    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

    
    
    

    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();
    

提交回复
热议问题