Sea*_* W. 87 java android airplane
我的应用程序中有代码可以检测Wi-Fi是否已主动连接.如果启用了飞行模式,该代码会触发RuntimeException.无论如何,我想在此模式下显示单独的错误消息.如何可靠地检测Android设备是否处于飞行模式?
Ale*_*voy 129
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
Tia*_*ago 92
通过扩展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;
}
}
Run Code Online (Sandbox Code Playgroud)
sax*_*xos 49
如果您不想轮询飞行模式是否处于活动状态,您可以为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>
Run Code Online (Sandbox Code Playgroud)
或以编程方式(所有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);
Run Code Online (Sandbox Code Playgroud)
并且如其他解决方案中所述,您可以在通知接收器时轮询飞行模式并抛出异常.
eld*_*jon 18
注册飞行模式时BroadcastReceiver(@saxos答案)我认为让飞机模式设置的状态立即离开是很有意义的Intent Extras,以避免调用Settings.Global或Settings.System:
@Override
public void onReceive(Context context, Intent intent) {
boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
if(isAirplaneModeOn){
// handle Airplane Mode on
} else {
// handle Airplane Mode off
}
}
Run Code Online (Sandbox Code Playgroud)
从这里:
public static boolean isAirplaneModeOn(Context context){
return Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,
0) != 0;
}
Run Code Online (Sandbox Code Playgroud)
为了摆脱折旧抱怨(当针对 API17+ 并且不太关心向后兼容性时),必须与以下内容进行比较Settings.Global.AIRPLANE_MODE_ON:
/**
* @param Context context
* @return boolean
**/
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0);
}
Run Code Online (Sandbox Code Playgroud)
在考虑较低的 API 时:
/**
* @param Context context
* @return boolean
**/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings({ "deprecation" })
private static boolean isAirplaneModeOn(Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){
/* API 17 and above */
return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
} else {
/* below */
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54216 次 |
| 最近记录: |