【问题标题】:How to know if Airplane Mode is TURNED ON or TURNED OFF in BroadcastReceiver?如何知道广播接收器中飞行模式是打开还是关闭?
【发布时间】:2016-09-05 06:39:24
【问题描述】:

我在使用此解决方案的其他问题中看到了多个答案,以了解飞行模式是否改变:

IntentFilter intentFilter = new      
IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED");

BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
            Log.d("AirplaneMode", "Service state changed");
      }
};

context.registerReceiver(receiver, intentFilter);

但是现在我想知道在 onReceive 方法中 Air Plane Mode 是打开还是关闭。

谢谢。

【问题讨论】:

    标签: android android-intent broadcastreceiver android-broadcast


    【解决方案1】:

    this answer 所述,您可以通过以下方式检查飞行模式状态:

    @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;
        }       
    }
    

    如果返回结果是true,那么飞行模式是ON,反之亦然,所以你的完整答案是:

    IntentFilter intentFilter = new      
    IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED");
    
    BroadcastReceiver receiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
                Log.d("AirplaneMode", "Service state changed");
                boolean airplane_active = isAirplaneModeOn(context);
          }
    };
    
    context.registerReceiver(receiver, intentFilter);
    

    【讨论】:

      【解决方案2】:

      试试这个代码。

      @Override
      public void onReceive(Context context, Intent intent) {
      
      boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
      if(isAirplaneModeOn){
      
         // AP mode is on
      } else {
         // AP mode is off
        }
      }
      

      【讨论】:

      • 我试过了,但它对我不起作用。还是谢谢你!
      【解决方案3】:

      试试这个。

      IntentFilter intentFilter = new      
      IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED");
      
      BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                  Log.d("AirplaneMode", "Service state changed");
                  boolean isAirplaneMode = isAirplaneModeOn(context);
            }
      };
      
      context.registerReceiver(receiver, intentFilter);
      
      private static boolean isAirplaneModeOn(Context context) {
      
         return Settings.System.getInt(context.getContentResolver(),
                 Settings.System.AIRPLANE_MODE_ON, 0) != 0;
      
      }
      

      注意

      在 Jelly Bean 4.2 中,此设置已移至 Settings.Global

      【讨论】:

      • 首选使用 Intent 类中的常量。在 Android 10 中是 public static final String ACTION_AIRPLANE_MODE_CHANGED = "android.intent.action.AIRPLANE_MODE";
      【解决方案4】:

      通过使用 ContentObserver 我们也可以得到

      private class SetupContentObserver extends ContentObserver {
      
      private final Uri mDeviceProvisioned = Settings.Global.getUriFor(
              Settings.Global.AIRPLANE_MODE_ON);
      
      public SetupContentObserver(Handler handler) {
          super(handler);
      }
      
      void register() {
          getContentResolver().registerContentObserver(mDeviceProvisioned, false, this, UserHandle.USER_ALL);
      }
      
      @Override
      public void onChange(boolean selfChange, Uri uri, int userId) {
          // do actula logic
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        • 2014-01-07
        • 2018-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多