PawelPredki
PawelPredki

Reputation: 754

Android AlarmManager BroadcastReceiver not working when user exits the application

I'm having some problems with a BroadcastReceiver that is supposed to react on an AlarmManager notification. I've read several threads on this subject but coulnd't find anything that would help. I must be missing something.

I set up the alarm the following way:

// Receiver intent
Intent intent = new Intent(mContext, CalendarAlarmReceiver.class);
intent.putExtra(CalendarAlarmReceiver.SHOW, show);
// Sender
PendingIntent sender = PendingIntent.getBroadcast(mContext, hashRequestCode(show), intent, PendingIntent.FLAG_UPDATE_CURRENT);
mPendingAlarmRequests.put(show.getChannel() + show.getName() + show.getStart().toGMTString(), sender);
// Get AlarmManager
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, show.getStart().getTime(), sender);

I created an CalendarAlarmReceiver class that extends BroadcastReceiver. In the onReceive() method I start a status bar notification that works fine if the application is running. However, if I exit the application by clicking the back button until it closes, the notifications no longer appear.

I'm not sure if the receiver is called at all and it's just the notification that doesn't work or if the receiver is not called.

It is registered in the manifest file:

<receiver android:name=".calendar.CalendarAlarmReceiver" >
</receiver>

I read that this may not be the receiver's falut but rather the alarm may be cancelled when the application stops. If that is the case, is the solution to simply set the alarm in a service that is working all the time in the background?

Upvotes: 2

Views: 2204

Answers (2)

VendettaDroid
VendettaDroid

Reputation: 3111

The receivers should have intent filters for the broadcast they are listening to. As Nikolay mentioned in the comment below that this is not necessary if you are sending explicit intent. So keep that in mind.

<receiver android:name=".calendar.CalendarAlarmReceiver">
    <intent-filter>
        <action android:name="REFRESH_THIS"/>
    </intent-filter>
</receiver>

Similary, you intent code should set an action called "REFRESH_THIS" while sending it.

Have a look at this example. It explains the whole process with nice explanation.

Upvotes: 0

Nikolay Elenkov
Nikolay Elenkov

Reputation: 52956

Alarms are managed by the OS and are generally not cleared when your application 'exits'. Put some logging in your receiver and watch to logcat output to find out if it is called. If it is, debug your notification code.

Upvotes: 4

Related Questions