3 回答
TA贡献1893条经验 获得超10个赞
/**
* Gets the state of Airplane Mode.
*
* @param context
* @return true if enabled.
*/
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
TA贡献1776条经验 获得超12个赞
通过扩展Alex的答案以包括SDK版本检查,我们可以:
/**
* Gets the state of Airplane Mode.
*
* @param context
* @return true if enabled.
*/
@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;
}
}
TA贡献1810条经验 获得超4个赞
而且,如果您不想轮询“飞行模式”是否处于活动状态,则可以为SERVICE_STATE Intent注册一个BroadcastReceiver并对其做出反应。
在您的ApplicationManifest(Android 8.0之前的版本)中:
<receiver android:enabled="true" android:name=".ConnectivityReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
或以编程方式(所有Android版本):
IntentFilter intentFilter = new IntentFilter("android.intent.action.AIRPLANE_MODE");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
}
};
context.registerReceiver(receiver, intentFilter);
并且,如其他解决方案中所述,您可以在收到通知时轮询飞行模式,并抛出异常。
添加回答
举报