Вопрос

It's unclear to me how to accomplish this, and I'm getting lost in the Android docs. I would assume this a common enough scenario though, so could someone show me the appropriate way to get hold of a BroadcastReceiver so I can unregister it (or point me in the right direction so I can educate myself) ?

SETTING THE ALARM:

registerReceiver(alarmHandler, new IntentFilter("com.blah.alarm"));
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent("com.blah.alarm"), 0);
AlarmManager am = (AlarmManager) (this.getSystemService( Context.ALARM_SERVICE ));
am.setRepeating(
        AlarmManager.RTC_WAKEUP, someCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

UNSETTING THE ALARM:

PendingIntent pi = PendingIntent.getBroadcast(
            this, 0, new Intent("com.blah.alarm", PendingIntent.FLAG_NO_CREATE);
if (pi != null) {
    AlarmManager am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    am.cancel(pi);
}
unregisterReceiver(????????); // how do I get alarmHandler ref ??
Это было полезно?

Решение

You'll need to save the alarmHandler reference in a variable somewhere so that you can get it when you need to call unregister()`. Android doesn't provide any method you can call to get a reference to it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top