本文介绍了BroadcastReceiver会多次触发(PROVIDERS_CHANGED_ACTION)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么 BroadcastReceiver
多次触发.
我的示例项目就像下面的代码
My Sample Project is like below code
机器人清单
<receiver
android:name=".LocationProvideListener"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED"/>
</intent-filter>
</receiver>
广播接收器
public class LocationProvideListener extends BroadcastReceiver {
private static final String TAG = "LocationProvideListener";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION))
{
// react on GPS provider change action
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isNetwork = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean isGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.e(TAG, "IsNetwork = " + (isNetwork ? "true" : "false"));
Log.e(TAG, "IsGPS = " + (isGPS ? "true" : "false"));
}
}
}
日志(打开位置时)
07-22 12:09:47.275 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsNetwork = false
07-22 12:09:47.275 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsGPS = true
07-22 12:09:47.778 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsNetwork = false
07-22 12:09:47.778 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsGPS = true
07-22 12:09:48.115 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsNetwork = false
07-22 12:09:48.115 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsGPS = true
日志(接受许可后)
07-22 12:09:55.412 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsNetwork = true
07-22 12:09:55.412 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsGPS = true
日志(关闭位置时)
07-22 12:10:04.856 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsNetwork = false
07-22 12:10:04.856 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsGPS = false
07-22 12:10:04.941 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsNetwork = false
07-22 12:10:04.941 22176-22176/com.example.checkLocationProvider E/LocationProvideListener﹕ IsGPS = false
推荐答案
使用以下概念来处理多个广播事件:
Use below concept for handling multiple broadcast event:
private BroadcastReceiver gpsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String action = intent.getAction();
if (!TextUtils.isEmpty(action) && action.equals(LocationManager.PROVIDERS_CHANGED_ACTION)) {
//Do your stuff on GPS status change
showDialogHandler.removeMessages(1);
showDialogHandler.sendEmptyMessageDelayed(1, 2000);
}
}
}
};
private Handler showDialogHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message message) {
if (message.what == 1) {
showDialogHandler.removeMessages(1);
// do your action
}
return true;
}
});
这篇关于BroadcastReceiver会多次触发(PROVIDERS_CHANGED_ACTION)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!