问题
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