1

我正在使用以下方法:

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)

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

4

2 回答 2

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 insted int 并且不要忘记添加权限

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

希望它可以帮助你

于 2014-12-30T10:16:09.080 回答
1

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

有关更多信息,请查看此飞行模式意图

低于 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" />
于 2014-12-30T10:16:21.677 回答