I am following the Android Location Deep Dive tutorial. In a part of the code, a receiver is registered like this.
IntentFilter locIntentFilter = new
IntentFilter(SINGLE_LOCATION_UPDATE_ACTION);
context.registerReceiver(singleUpdateReceiver, locIntentFilter);
In another part of the code, we find the following;
singleUpatePI = PendingIntent.getBroadcast(context, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
locationManager.requestSingleUpdate(criteria, singleUpatePI);
The Idea is that the LocationManager wil fire the SingleUpdatePI Pending intent when a location of the given accuracy criteria is available. My receiver called “singleUpdateReceiver” will be invoked, since I registered it earlier
It works, but my SingleUpdateReceiver’s onReceive method is invoked twice(!). This causes problems because I take care to unregister it the first time. No extras are available in any of the invocations.
Can anyone explain why locationManager.requestSingleUpdate(..) will fire the pending intent twice? This causes some problems because the first thing I do in my receiver is to unregister it like this
context.unregisterReceiver(singleUpdateReceiver);
On the second invocaiton, the receiver is not registered and an exception is thrown.
Tested on Android 2.3.3 HTC Sensation.
I believe this is happening because of the following line…
It should be…
I haven’t noticed the
onReceive(...)method getting called twice after this change. Hope this helps.