广播接收器onReceive在位置变化时触发两次

时间:2018-05-22 12:54:14

标签: android kotlin observable android-broadcastreceiver

我想知道每当用户关闭他的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"
}}

My Observable

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)
}

3 个答案:

答案 0 :(得分:1)

我认为这个问题只出现在某些设备上(虽然我对此并不是100%肯定,但是通过BroadcastReceiver收听网络更改就是这种情况,这样才有意义。) / p>

无论如何,最简单的解决方案是制作enum,其中包含State的当前GPS。仅当状态更改通知Observer 时。这样onReceive可以调用1000次,但Observer只会被通知一次。

答案 1 :(得分:0)

我解决了:

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

答案 2 :(得分:0)

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)