简体   繁体   English

在Android中实现飞行模式

[英]Implement airplane mode in android

I am making an app in which I want to display a pop up if airplane mode is active but when I use the following code, it switches my app to the airplane mode means my device goes to airplane mode. 我正在制作一个应用程序,如果飞行模式处于活动状态,我想在其中显示弹出窗口,但是当我使用以下代码时,它将我的应用程序切换到飞行模式,这意味着我的设备进入了飞行模式。 Where as I want if airplane mode is active, a device should give a pop-up only. 如果飞行模式处于活动状态,我希望在哪里显示设备,但只能弹出一个窗口。 My code is as follows: 我的代码如下:

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

   }

At a guess I'd say your issue lies in the fact your integer to boolean code is incorrect. 猜想我会说您的问题出在您的整数到布尔代码不正确的事实上。

Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM