在android中实现飞行模式

时间:2012-03-12 11:33:13

标签: android airplane

我正在创建一个应用程序,我希望在飞行模式处于活动状态时显示弹出窗口但是当我使用以下代码时,它将我的应用程序切换到飞行模式意味着我的设备进入飞行模式。如果我想要飞机模式处于活动状态,设备应仅提供弹出窗口。我的代码如下:

public void airplane() {
    final boolean isEnabled = Settings.System.getInt(
              getContentResolver(), 
              Settings.System.AIRPLANE_MODE_ON, 1) == 0;

        // toggle airplane mode
        Settings.System.putInt(
              getContentResolver(),
              Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0: 1);


        // Post an intent to reload
    //  Toast tt=Toast.makeText(getApplicationContext(), "Your phone is in airplane mode", Toast.LENGTH_SHORT);
    //  tt.show();
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", isEnabled);
        sendBroadcast(intent);

        IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");

        BroadcastReceiver receiver = new BroadcastReceiver() {
              @Override
              public void onReceive(Context context, Intent intent) {
                    Log.d("AirplaneMode", "Service state changed");
              }
        };

        context.registerReceiver(receiver, intentFilter);

   }

1 个答案:

答案 0 :(得分:0)

我猜你的问题在于你的整数到布尔代码是不正确的。

尝试:

final boolean isEnabled = (Settings.System.getInt(
              getContentResolver(), 
              Settings.System.AIRPLANE_MODE_ON, 1)) != 0;
相关问题