BroadcastReceiver多次触发(PROVIDERS_CHANGED_ACTION)

时间:2015-07-22 05:38:59

标签: android broadcastreceiver

为什么BroadcastReceiver多次触发。

我的示例项目如下面的代码

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

记录(当地点开启时)

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

1 个答案:

答案 0 :(得分:2)

使用以下概念来处理多个广播事件:

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