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
To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.
Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.
Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.
registerReceiver() method. The implementing class for a receiver extends the BroadcastReceiver class. If the event for which the broadcast receiver has registered happens, the onReceive() method of the receiver is called by the Android system.
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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With