Note that there are some explanatory texts on larger screens.

plurals
  1. POLocationManager.removeUpdates(listener) not removing location listener
    primarykey
    data
    text
    <p>My app scenario is that I want to track employees location. I have a broadcastreceiver which listens device boot broadcast and register an alarm manager. When alarm manager ticks, it registers two location listeners, one to listen to gps and other for network. I want that when I got first location update in onLocationChange() method, save location and unregister that location listener so that when alarm manager ticks again, it does not get duplicate. To unregister, I have location listeners as static objects to access them in onLocationChange(). But I have found that it is NOT removing location listener. Here is my code example:</p> <pre><code> public class BootTimeServiceActivator extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Calendar calendar = Calendar.getInstance(); AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent mIntent = new Intent(context, MyBroadCastReceiver.class); PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT); am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20 * 60 * 1000, mPendingIntent); } } //.......... public class MyBroadCastReceiver extends BroadcastReceiver{ public static LocationManager locationManager; public static MyLocationListener networkLocationListener; public static MyLocationListener gpsLocationListener; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "Alarm has been called...", Toast.LENGTH_SHORT).show(); initLocationListeners(context); registerLocationListeners(); } public void initLocationListeners(Context context) { locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); networkLocationListener = new MyLocationListener(context); gpsLocationListener = new MyLocationListener(context); } public void registerLocationListeners() { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, gpsLocationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, gpsLocationListener); } } \\..... public class MyLocationListener implements LocationListener { Context context; public MyLocationListener(Context context) { this.context = context; } @Override public void onLocationChanged(Location location) { if(location != null) { SDCardService sdService = new SDCardService(context); try { sdService.logToDB(location); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.d("provider",location.getProvider()); if(location.getProvider().equals(LocationManager.GPS_PROVIDER)) { MyBroadCastReceiver.locationManager.removeUpdates(MyBroadCastReceiver.gpsLocationListener); } else if(location.getProvider().equals(LocationManager.NETWORK_PROVIDER)) { MyBroadCastReceiver.locationManager.removeUpdates(MyBroadCastReceiver.networkLocationListener); } } } </code></pre> <p>Can anyone guide me where I am wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload