如何在有根设备上切换飞行模式?

时间:2014-12-30 10:09:15

标签: java android android-settings

我使用以下方法:

private void toggleAirplaneMode() throws Exception {        
    // read the airplane mode setting
    boolean isEnabled = android.provider.Settings.System.getInt(
          getContentResolver(), 
          android.provider.Settings.Global.AIRPLANE_MODE_ON, 0) == 1;

    // toggle airplane mode
    android.provider.Settings.System.putInt(
          getContentResolver(),
          android.provider.Settings.Global.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);
}

获得以下异常:

12-30 09:49:05.875: D/tag(5876): android.os.Parcel.readException(Parcel.java:1540)
12-30 09:49:05.875: D/tag(5876): android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
12-30 09:49:05.875: D/tag(5876): android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
12-30 09:49:05.875: D/tag(5876): android.content.ContentProviderProxy.call(ContentProviderNative.java:643)
12-30 09:49:05.875: D/tag(5876): android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1094)
12-30 09:49:05.876: D/tag(5876): android.provider.Settings$Global.putStringForUser(Settings.java:6827)
12-30 09:49:05.876: D/tag(5876): android.provider.Settings$Global.putString(Settings.java:6811)
12-30 09:49:05.876: D/tag(5876): android.provider.Settings$Global.putInt(Settings.java:6905)

我哪里出错?还有其他出路???

2 个答案:

答案 0 :(得分:1)

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

将String放入int中 并且不要忘记添加权限

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

希望它可以帮到你

答案 1 :(得分:1)

从Android 4.2飞机模式更改意图仅受保护系统应用程序可以更改它。因此它不适用于其他应用程序。

有关详情,请查看此Aeroplane mode Intent

低于4.2你可以像

那样使用飞机模式

您可以获得类似

的飞行模式状态
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;
    }
}

您可以更改飞机模式,如

@SuppressWarnings("deprecation")
public static void switchAeroplaneMode(Context context, boolean isEnabled) {
    if (isEnabled == isAirplaneModeOn(context)) {
        return;
    }
    if (isAeroplaneModeSupports()) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, isEnabled ? 1 : 0);

        // Post an intent to reload.
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", !isEnabled);
        context.sendBroadcast(intent);
    }
}

public static boolean isAeroplaneModeSupports() {
    return Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1;
}

在清单

中添加权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />