我的应用程序中有代码可以检测 Wi-Fi 是否已主动连接。如果启用了飞行模式,该代码将触发 RuntimeException。无论如何,我想在此模式下显示单独的错误消息。如何可靠地检测 Android 设备是否处于飞行模式?
13 回答
/**
* 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;
}
通过扩展 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(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);
如其他解决方案所述,您可以在通知接收器时轮询飞行模式并抛出异常。
在注册飞行模式时BroadcastReceiver
(@saxos 回答),我认为立即从 获取飞行模式设置的状态是很有意义的Intent Extras
,以避免调用Settings.Global
or 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;
}
为了摆脱折旧投诉(针对 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;
}
}
在奥利奥请不要使用飞行模式broadCastReceiver。这是一个隐含的意图。它已被删除。这是当前的例外列表。它目前不在列表中,因此应该无法接收数据。认为它死了。
正如上面另一个用户所说,使用以下代码:
@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 文件
在应用程序打开时注册广播接收器无需在清单中添加代码,如果您仅在您的活动打开时采取操作,例如在您访问互联网时检查飞行模式是否打开等
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();
}
从 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;
}
我写了这门课,可能会有所帮助。它不会直接返回布尔值来告诉您飞行模式是启用还是禁用,但它会在飞行模式从一种更改为另一种时通知您。
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();
}
这是唯一对我有用的东西(API 27):
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
this.registerReceiver(br, filter);
br
你的广播接收器在哪里。我相信,随着最近许可的变化,现在两者ConnectivityManager.CONNECTIVITY_ACTION
都是Intent.ACTION_AIRPLANE_MODE_CHANGED
必要的。
自 Jelly Bean(构建代码 17)以来,此字段已移至全局设置。因此,为了获得最佳的兼容性和稳健性,我们必须同时处理这两种情况。以下示例是用 Kotlin 编写的。
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
}
注意:如果不支持 Jelly Bean 之前的版本,可以省略 if 子句。
您在引用 时获得的值与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
}
您可以检查互联网是否已打开
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;
}
}