广播接收器 onReceive 在位置更改时触发两次
Broadcast receiver onReceive fires twice on location change
我想知道用户何时关闭他的 GPS。我想在不同的活动中了解该动作。我制作了 Broadcast Receiver 来监听 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"
}}
我的 Observable
class ObservableGpsState: Observable(){
fun updateValue(data: Any) {
synchronized(this) {
setChanged()
notifyObservers(data)
}
}
companion object {
private val instance = ObservableGpsState()
fun getInstance(): ObservableGpsState {
return instance
}
}}
而且(我猜)对我来说很重要 Activity:
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)
}
我假设这个问题只出现在某些设备上(虽然我不是 100% 确定,但通过 BroadcastReceiver
监听网络变化时就是这种情况,所以这是有道理的) .
无论如何,最简单的解决方案是创建一个 enum
来保存 GPS
的当前 State
。仅当 State 更改通知 Observer。这样 onReceive
可以被调用 1000 次,但你的 Observer
只会收到一次通知。
我解决了:
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);
}
}
Android 中有多个位置提供商。因此,您正在收听的事件对应于提供者更改,而不是位置激活。参见 。
改用 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)
我想知道用户何时关闭他的 GPS。我想在不同的活动中了解该动作。我制作了 Broadcast Receiver 来监听 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"
}}
我的 Observable
class ObservableGpsState: Observable(){
fun updateValue(data: Any) {
synchronized(this) {
setChanged()
notifyObservers(data)
}
}
companion object {
private val instance = ObservableGpsState()
fun getInstance(): ObservableGpsState {
return instance
}
}}
而且(我猜)对我来说很重要 Activity:
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)
}
我假设这个问题只出现在某些设备上(虽然我不是 100% 确定,但通过 BroadcastReceiver
监听网络变化时就是这种情况,所以这是有道理的) .
无论如何,最简单的解决方案是创建一个 enum
来保存 GPS
的当前 State
。仅当 State 更改通知 Observer。这样 onReceive
可以被调用 1000 次,但你的 Observer
只会收到一次通知。
我解决了:
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);
}
}
Android 中有多个位置提供商。因此,您正在收听的事件对应于提供者更改,而不是位置激活。参见 。
改用 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)