Can't stop a service started with alarmManager

你离开我真会死。 提交于 2019-12-12 13:18:57

问题


I'm having problems to cancel an alarm and a service started with it.

I set the alarm in a activity:

public class AlarmStarter extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      Intent locationPollerIntent = new Intent(this, LocationPoller.class);     
      PendingIntent pendingIntent = PendingIntent.getBroadcast
                (getApplicationContext(), REQUEST_CODE, locationPollerIntent, 0);

      alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
      alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                             SystemClock.elapsedRealtime(), 6000, pendingIntent);
    }
}

The LocationPoller intent will start a service to obtain the location of the device. So I use an alarmManager to repeat this search in certain frequency.

After that when I get my location on a BroadcastReceiver, I call another Activity to show the location and fire a notification.

At this time, I would like to stop searching for the location. So I try to cancel the alarmManager.

public class Notifier extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm_notifier);

        findViewById(R.id.btTurnOffAlarm).
                               setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intentToStop = new Intent(this, LocationPoller.class);
                PendingIntent pendingIntentToStop = PendingIntent.getBroadcast 
                       (getApplicationContext(), REQUEST_CODE, intentToStop, 0);

                AlarmManager alarmManager = (AlarmManager)
                                getSystemService(ALARM_SERVICE);
                alarmManager.cancel(pendingIntentToStop);
            }
        });
    }
}

But this is not working, it continues to search for the location.

Any clues?

Thanks.


回答1:


You have to make the intent unique.

sth similar to:

locationPollerIntent.setData((Uri.parse("YOUR UNIQUE ID FOR THIS INTENT")));

AlarmManager will check the data field of an intent when getting back the right alarm for you. just set a unique Uri to the data of the intent.

so when you get back your alarm, do sth like:

intentToStop.setData((Uri.parse("YOUR SAME UNIQUE ID FOR THIS INTENT")));

the alarm should be cancelled after.




回答2:


REQUEST_CODE needs to be the same in both places; you do not show where that is defined in your code listings.

Also, you do not need getApplicationContext() here -- this will work just fine.




回答3:


You have to pass the same instance.

public static final Uri MYURI = Uri.parse("Some_Uri");
[...]
locationPollerIntent.setData(MyClass.MYURI);
[...]
intentToStop.setData(MyClass.MYURI);

If you have two different references each time you create the Intent, it won't work.

More detailed example here:

http://malubu.wordpress.com/2012/06/05/take-your-time-widgets-and-alarmmanager/




回答4:


I had the same issue, its not about having the same instance of the Intent "locationPollerIntent" and "intentToStop" but you must have the same instance for the PendingIntent.

After stoping the AlarmManager, the service stops too.

    static PendingIntent instance;

public static PendingIntent getInstance(Context context) {

    if (instance == null)
    {
        Intent intent = new Intent(context, LocationPoller.class); 
        instance = PendingIntent.getService(context, REQUEST_CODE, intent, 0);
    }

    return instance;
}


来源:https://stackoverflow.com/questions/7605685/cant-stop-a-service-started-with-alarmmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!