【问题标题】:Broadcast receiver onReceive fires twice on location change广播接收器 onReceive 在位置更改时触发两次
【发布时间】:2018-05-22 12:54:13
【问题描述】:

我想知道用户何时关闭他的 GPS。我想在不同的活动中了解该行动。我制作了广播接收器来监听 GPS 状态的变化。但几乎总是当我关闭 GPS 时,我的 updateValue 函数会被触发两次。当用户关闭他的 gps 时,如何获得一次通知?我做错了什么?下面是我的代码。

class GpsStatusReceiver : BroadcastReceiver() {
var observableGpsState: ObservableGpsState? = null

override fun onReceive(context: Context?, intent: Intent?) {
    val locationManager = context?.getSystemService(Context.LOCATION_SERVICE) as LocationManager
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        //GPS Turned on,OK..
    } else {
        if (observableGpsState == null)
            observableGpsState = ObservableGpsState.getInstance()
        observableGpsState?.updateValue(GPS_STATUS_TURNED_OFF)
    }
}

companion object {
    val GPS_STATUS_TURNED_OFF = "GPS_TURNED_OFF"
}}

我的观察者

class ObservableGpsState: Observable(){

fun updateValue(data: Any) {
    synchronized(this) {
        setChanged()
        notifyObservers(data)
    }
}
companion object {
    private val instance = ObservableGpsState()
    fun getInstance(): ObservableGpsState {
        return instance
    }
}}

而且(我猜)在我的活动中很重要:

protected lateinit var observer: Observable

private fun registerGpsObserver(){
    observer = ObservableGpsState.getInstance()
    observer.addObserver(this)
}
private fun unregisterGpsObserver(){
    observer.deleteObserver(this)
}

override fun update(o: Observable?, arg: Any?) {
    MyApplication.get(baseContext).enableGpsDialog(this)
}

【问题讨论】:

    标签: android kotlin observable android-broadcastreceiver


    【解决方案1】:

    我认为这个问题只出现在某些设备上(虽然我不是 100% 确定这一点,但是通过BroadcastReceiver 监听网络变化时会出现这种情况,所以这是有道理的)。

    无论如何,最简单的解决方案是创建一个enum,它包含您的GPS 的当前State。仅当状态变化通知观察者。这样,onReceive 可以被调用 1000 次,但您的 Observer 只会收到一次通知。

    【讨论】:

    • 我怎样才能做到这一点?
    【解决方案2】:

    我解决了:

     BroadcastReceiver gpsSwitchStateReceiver = new BroadcastReceiver() {
        //here you can check your current state when instanciate it.
            boolean oldLocationEnabled = isLocationSystemEnabled();
    
            @Override
            public void onReceive(Context context, Intent intent) {
                try {
                    String action;
                    if (intent != null && (action = intent.getAction()) != null && action.matches(LocationManager.PROVIDERS_CHANGED_ACTION)) {
                        boolean locationEnabled = isLocationSystemEnabled();
    //this validation ensures that it is executed only once (since the receiver is executed in the UIThread) 
                        if (oldLocationEnabled != locationEnabled) {
                            if (locationEnabled) {
                            //your code here 
                            } else {
                            //when dont have location here
                            }
                        }
                        oldLocationEnabled = locationEnabled;
                    }
                } catch (Exception ignored) {}
            }
        };
    
    public boolean isLocationSystemEnabled() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            LocationManager locationManager = (LocationManager) ApplicationLoader.appContext.getSystemService(Context.LOCATION_SERVICE);
            boolean         isGPSEnabled    = false;
            boolean         isNetEnabled    = false;
            if (locationManager != null) {
                isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                isNetEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            }
            return isGPSEnabled || isNetEnabled;
        } else {
            String allowedLocationProviders = Settings.System.getString(ApplicationLoader.appContext.getContentResolver(), Settings.System.LOCATION_PROVIDERS_ALLOWED);
            if (allowedLocationProviders == null) {
                allowedLocationProviders = "";
            }
            return allowedLocationProviders.contains(LocationManager.GPS_PROVIDER);
        }
    }
    

    【讨论】:

      【解决方案3】:

      Android 中有多个位置提供程序。因此,您正在收听的事件对应于提供者更改,而不是位置激活。见https://stackoverflow.com/a/6775456/13941008

      改用LocationManager.MODE_CHANGED_ACTION 并使用LocationManager.EXTRA_LOCATION_ENABLED 直接从Intent 参数检索当前位置激活状态。示例:

      val locationListener = object : BroadcastReceiver() {
         override fun onReceive(context: Context?, intent: Intent) {
            when(val action = intent.action){
               LocationManager.MODE_CHANGED_ACTION -> {
                  val locationState = intent.getBooleanExtra(
                     LocationManager.EXTRA_LOCATION_ENABLED,
                     false
                  )
                // Do something using locationState variable
                } else -> {
                   Log.d(TAG, "Unsupported action received $action")
                }
            }
         }
      }
      
      val intentFilter = IntentFilter(LocationManager.MODE_CHANGED_ACTION)
      context.registerReceiver(locationListener, intentFilter)
      

      最后,不要忘记在不再需要接收器时取消注册它。示例:

      context.unregisterReceiver(locationListener)
      

      【讨论】:

        猜你喜欢
        • 2011-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多