我在我的应用程序中有一段代码,可以检测Wi-Fi是否处于活动连接状态。如果飞行模式已启用,则该代码会触发RuntimeException异常。无论如何,我希望在此模式下显示一个单独的错误消息。如何可靠地检测Android设备是否处于飞行模式?
我在我的应用程序中有一段代码,可以检测Wi-Fi是否处于活动连接状态。如果飞行模式已启用,则该代码会触发RuntimeException异常。无论如何,我希望在此模式下显示一个单独的错误消息。如何可靠地检测Android设备是否处于飞行模式?
/**
* 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;
}
android.intent.action.AIRPLANE_MODE
的方式调用它时,它会产生不确定的结果,因为模式更改需要时间才能完成。如果您想这样做,请检查Intent.ACTION_AIRPLANE_MODE_CHANGED
。 - Noumenon通过扩展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;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
,否则Eclipse无法编译这个。 - Noumenon如果你不想轮询Airplane Mode
是否开启,那么你可以注册一个接收器来监听SERVICE_STATE
意图并作出反应。
在你的中进行注册(适用于 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);
正如其他解决方案所描述的那样,当您的接收器被通知并且出现异常时,您可以轮询飞行模式。
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
或 <action android:name="android.intent.action.AIRPLANE_MODE" />
。 - Nappyboolean isPlaneModeOn = intent.getBooleanExtra("state", false);
如果用户已经打开了飞行模式,则布尔值 isPlaneModeOn
将为 true
,如果它被关闭,则为 false
。 - Sudara在注册飞行模式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
}
}
以下内容来自这里:
public static boolean isAirplaneModeOn(Context context){
return Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,
0) != 0;
}
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);
}
在考虑较低的 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;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings({ "deprecation" })
public 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;
}
}
清单代码:
<receiver android:name=".airplanemodecheck" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"></action>
</intent-filter>
</receiver>
Java代码:广播接收器Java文件
if(Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0)== 0)
{
Toast.makeText(context, "AIRPLANE MODE Off", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "AIRPLANE MODE On", Toast.LENGTH_SHORT).show();
}
Java代码:活动java文件
当应用程序打开时注册广播接收器,如果您只在打开活动时执行某个操作,例如在访问Internet时检查飞行模式是否打开或关闭等,则无需在清单中添加代码。
airplanemodecheck reciver;
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
reciver = new airplanemodecheck();
registerReceiver(reciver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(reciver);
}
Java 代码:广播接收器 Java 文件
if(Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0)== 0)
{
Toast.makeText(context, "AIRPLANE MODE Off", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "AIRPLANE MODE On", Toast.LENGTH_SHORT).show();
}
fun isInAirplane(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Settings.Global.getInt(
context.contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0
)
} else {
Settings.System.getInt(
context.contentResolver, Settings.System.AIRPLANE_MODE_ON, 0
)
} != 0
}
Settings.System.AIRPLANE_MODE_ON
时获取的值与在Global.*下找到的值相同。 /**
* @deprecated Use {@link android.provider.Settings.Global#AIRPLANE_MODE_ON} instead
*/
@Deprecated
public static final String AIRPLANE_MODE_ON = Global.AIRPLANE_MODE_ON;
这是之前代码的高于果冻豆版本。
fun isInAirplane(context: Context): Boolean {
return Settings.Global.getInt(
context.contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0
) != 0
}
从API Level - 17开始
/**
* Gets the state of Airplane Mode.
*
* @param context
* @return true if enabled.
*/
private static boolean isAirplaneModeOn(Context context) {
return Settings.Global.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}