0

I modified the OMGStop() method to something more like this:

public void cancelUpdates() {
    //TODO potential bug here
    if(pi == null)
        setPendingIntent();
    mgr.cancel(pi);     

    //Should one of these work?
    stopService(new Intent(applicationContext, LocationPoller.class));
    stopService(new Intent(applicationContext, LocationPollerService.class));

I'm storing pi (PendingIntent) as a member in my activity class. And this works fine to remove the PendingIntent from the AlarmManager.

However...

I would like to be able to stop the current location poll if there is one going on. Is it possible with your current design? I thought I could just stop the service, but the GPS continues to run.

Basically what i'm trying to do is stop everything when the user (me on my trip) changes a preference (such as the timeout, or USE GPS or update period. And then recreate everything with the new values.

Thanks,

Great code BTW - Exactly what I want for tracking my cross country journey :)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
mparkes
  • 332
  • 3
  • 12

2 Answers2

2

I faced the same issue. - On occasion I let my mousepointer hover over "mgr.setRepeating(..)" and read some of Eclipse's (Indigo) hints: "If there is already an alarm scheduled for the same IntentSender, it will first be canceled." But the IntentSender could be gone by then. This led me to the following "solution" (in original CommonsWare code):

if(pi == null) {
  Intent i = new Intent(this, LocationPoller.class);
  pi = PendingIntent.getBroadcast(this, 0, i, 0);
  mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() + 1000, PERIOD, pi);
}
mgr.cancel();

By adding 1000 msec I tried to make sure the AlarmManager has no chance to start before being hit by a cancel().

HTH, regards.

PS: I'd like to quote mparkes: "Great code"!

resLin
  • 41
  • 5
0

Is it possible with your current design?

No, sorry. That's theoretically possible to add, but probably a bit tricky, and definitely not there at the moment.

Basically what i'm trying to do is stop everything when the user (me on my trip) changes a preference (such as the timeout, or USE GPS or update period. And then recreate everything with the new values.

That is a perfectly reasonable concept, just not what LocationPoller supports. LocationPoller was designed more for the "check every hour" sorts of scenarios, where it is statistically unlikely that a check is going on while the user happens to be manipulating your app's UI.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ok cool, thanks... It works great! I fixed a bug with my UI by adding the FLAG_CANCEL_CURRENT to the pending intent. I then also, disable the active UI checkbox and cancel Alarms when user changes any of the UI settings. This forces them to restart manually, and with that FLAG all the new settings are updated ( before I was trying to cancel and restart alarms in the same preference listenner) – mparkes Aug 21 '12 at 20:19