Turbozanik Turbozanik - 6 months ago 42
Android Question

Can't fix IntentService for notification with AlarmManager

I ma trying to handle it this way: in broadCastReciever start AlarmManager repeated action, it will send intents to IntentService, service writes log. Now as i see from the log, breadCastReciver recives intent, starts AlarmManager, but IntentService never fires. What can be wrong here?


<receiver android:name=".wakefullBroadcastReciever.SimpleWakefulReciever" android:enabled="true" android:exported="false">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="START"/>

<action android:name="NOTIFY_INTENT" />


public class SimpleWakefulReciever extends WakefulBroadcastReceiver {

public void onReceive(Context context, Intent intent) {
if (!App.isRunning) {
Log.d("wakefull", "start");
Intent startIntent = new Intent(context, NotificationWakefulIntentService.class);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
SystemClock.elapsedRealtime() + 3000, 5000, startPIntent);
App.isRunning = true;


public class NotificationWakefulIntentService extends IntentService {

public NotificationWakefulIntentService() {

protected void onHandleIntent(Intent intent) {


You're defining an explicit Service Intent, but calling getBroadcast() instead of getService().

Change the following:

PendingIntent startPIntent = PendingIntent
    .getBroadcast(context, 0, startIntent, 0);

To this:

PendingIntent startPIntent = PendingIntent
    .getService(context, 0, startIntent, 0);

Also, this is not how WakefulBroadcastReceiver works. It's a helper class and its purpose is to provide a WakeLock until your Service finishes its work.

You achieve nothing by simply extending WakefulBroadcastReceiver, a WakeLock is guaranteed during onReceive() anyway.

To answer your comment below:

You should set an exact alarm to fire every hour (check out this answer), start your IntentService from onReceive() by calling WakefulBroadcastReceiver.startWakefulService(), do your stuff in onHandleIntent() and call WakefulBroadcastReceiver.completeWakefulIntent() when finished.