我已经在此解决方案中看到了其他问题的多个答案,以了解飞机模式是否已更改:
IntentFilter intentFilter = new
IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
}
};
context.registerReceiver(receiver, intentFilter);
但现在我想知道onReceive方法是否打开或关闭Air Plane模式。
谢谢。
答案 0 :(得分:3)
如this answer所述,您可以通过以下方式检查飞机模式状态:
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
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;
}
}
如果返回的结果为true
,则飞行模式为 ON ,反之亦然,因此您的完整答案将为:
IntentFilter intentFilter = new
IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
boolean airplane_active = isAirplaneModeOn(context);
}
};
context.registerReceiver(receiver, intentFilter);
答案 1 :(得分:1)
试试这个。
IntentFilter intentFilter = new
IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
boolean isAirplaneMode = isAirplaneModeOn(context);
}
};
context.registerReceiver(receiver, intentFilter);
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}
在Jelly Bean 4.2中,此设置已移至Settings.Global
答案 2 :(得分:1)
试试这段代码。
@Override
public void onReceive(Context context, Intent intent) {
boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
if(isAirplaneModeOn){
// AP mode is on
} else {
// AP mode is off
}
}