如何检测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.System.AIRPLANE_MODE_ON, 0) != 0; }
通过扩展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; } }
如果您不想轮询飞行模式是否激活,您可以为SERVICE_STATE Intent注册BroadcastReceiver并对其作出反应。
无论是在您的ApplicationManifest:
<receiver android:enabled="true" android:name=".ConnectivityReceiver"> <intent-filter> <action android:name="android.intent.action.AIRPLANE_MODE"/> </intent-filter> </receiver>
或以编程方式:
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);
正如其他解决scheme中所述,您可以在收到通知的情况下轮询飞行模式并抛出exception。
在注册飞行模式BroadcastReceiver
(@saxos答案)时,为了避免调用Settings.Global
或Settings.System
我认为将Airplane Mode的状态设置为远离Intent Extras
是非常有意义的:
@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; }
为了摆脱折旧投诉(当瞄准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); }
在考虑较低的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; } }
静态广播接收机
清单代码:
<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代码:Broadcast Receiver 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(); }
要么
dynamic广播接收机
Java代码:活动java文件
在应用程序打开时注册广播接收器,无需在清单中添加代码,只有当您的活动如打开或closures当您访问互联网等活动时才采取行动
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代码:Broadcast Receiver 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(); }
从API级别 – 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; }
我写这个class可能会有所帮助。 它不直接返回一个布尔值来告诉你飞行模式是启用还是禁用,但是当飞行模式从一个模式改变到另一个时,它会通知你。
public abstract class AirplaneModeReceiver extends BroadcastReceiver { private Context context; /** * Initialize tihe reciever with a Context object. * @param context */ public AirplaneModeReceiver(Context context) { this.context = context; } /** * Receiver for airplane mode status updates. * * @param context * @param intent */ @Override public void onReceive(Context context, Intent intent) { if(Settings.System.getInt( context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0 ) == 0) { airplaneModeChanged(false); } else { airplaneModeChanged(true); } } /** * Used to register the airplane mode reciever. */ public void register() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); context.registerReceiver(this, intentFilter); } /** * Used to unregister the airplane mode reciever. */ public void unregister() { context.unregisterReceiver(this); } /** * Called when airplane mode is changed. * * @param enabled */ public abstract void airplaneModeChanged(boolean enabled); }
用法
// Create an AirplaneModeReceiver AirplaneModeReceiver airplaneModeReceiver; @Override protected void onResume() { super.onResume(); // Initialize the AirplaneModeReceiver in your onResume function // passing it a context and overriding the callback function airplaneModeReceiver = new AirplaneModeReceiver(this) { @Override public void airplaneModeChanged(boolean enabled) { Log.i( "AirplaneModeReceiver", "Airplane mode changed to: " + ((active) ? "ACTIVE" : "NOT ACTIVE") ); } }; // Register the AirplaneModeReceiver airplaneModeReceiver.register(); } @Override protected void onStop() { super.onStop(); // Unregister the AirplaneModeReceiver if (airplaneModeReceiver != null) airplaneModeReceiver.unregister(); }
你可以检查互联网是否打开
public class ConnectionDetector { private Context _context; public ConnectionDetector(Context context){ this._context = context; } public boolean isConnectingToInternet(){ ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) for (int i = 0; i < info.length; i++) if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } return false; }
}