BroadcastReceiver fires multiple times (PROVIDERS_CHANGED_ACTION)

女生的网名这么多〃 提交于 2021-02-07 12:55:42

问题


Why BroadcastReceiver was trigger multiple time.

My Sample Project is like below code

ANDROID MANIFEST

    <receiver
            android:name=".LocationProvideListener"
            android:enabled="true"
            android:exported="true">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED"/>
        </intent-filter>
    </receiver>

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

LOG (WHEN TURN ON LOCATION)

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

LOG (WHEN ACCEPT PERMISSION)

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

LOG (WHEN TURN OFF LOCATION)

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

回答1:


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


来源:https://stackoverflow.com/questions/31554676/broadcastreceiver-fires-multiple-times-providers-changed-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!