如何在Android上检测飞机模式?

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)

  • 在Jelly Bean 4.2中,此设置已移至"Settings.Global". (31认同)
  • 只是一个提示:!= 0返回false(飞行模式关闭)和== 0返回true(飞行模式打开) (7认同)

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)

  • 除非在方法之前添加`@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)`,否则Eclipse不会编译它. (5认同)

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)

并且如其他解决方案中所述,您可以在通知接收器时轮询飞行模式并抛出异常.

  • mpstx:或者使用:`IntentFilter intentFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);`/`<action android:name ="android.intent.action.AIRPLANE_MODE"/>` (11认同)
  • 使用Intent.ACTION_AIRPLANE_MODE_CHANGED (4认同)
  • 如果打开或关闭飞机模式,我们也可以在我们收到的意图中使用布尔额外值.`boolean isPlaneModeOn = intent.getBooleanExtra("state",false);`如果用户打开了飞机模式,则布尔值"isPlaneModeOn"将为"true";如果关闭,则为"false" (3认同)
  • 注意:由于还有其他SERVICE_STATE通知,您必须在接收SERVICE_STATE通知之前检查并存储飞行模式的状态,然后在收到服务状态通知时检查它的状态,然后比较两者 - 要知道如果飞机模式实际改变了 (2认同)
  • 对于此解决方案,您将需要权限:<uses-permission android:name ="android.permission.READ_PHONE_STATE"/> (2认同)

eld*_*jon 18

注册飞行模式时BroadcastReceiver(@saxos答案)我认为让飞机模式设置的状态立即离开是很有意义的Intent Extras,以避免调用Settings.GlobalSettings.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)

  • 这是检索实际飞行模式状态的最有效方法.这应该得到投票并成为新接受的答案.+1用于阅读有关此"状态"意图的文档.我测试过,它运行正常. (2认同)

Pre*_*gha 7

这里:

 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)


Mar*_*ler 5

为了摆脱折旧抱怨(当针对 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)