Implement airplane mode in android

风流意气都作罢 提交于 2019-12-13 07:08:26

问题


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

   }

回答1:


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;


来源:https://stackoverflow.com/questions/9666253/implement-airplane-mode-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!