manabreak manabreak - 1 month ago 7
Android Question

AlarmManager doesn't trigger a WakefulBroadcastReceiver

I'm trying to launch an intent service using an AlarmManager and a WakefulBroadcastReceiver. I can launch the service using

sendBroadcast()
and it works as it should, but I can't get it to launch with AlarmManager.

The code for launching it is like this:

@Override
protected void onPause()
{
super.onPause();

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, Notifier.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);

am.set(RTC_WAKEUP, System.currentTimeMillis(), pi);
}


The
Notifier
class is like this:

public class Notifier extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("Notifier", "onReceive()");
ComponentName comp = new ComponentName(context.getPackageName(), NotifierService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
}
}


And the
NotifierService
is like this:

public class NotifierService extends IntentService
{
private static final String TAG = "NotifService";

public NotifierService()
{
super("NotifierService");
}

@Override
protected void onHandleIntent(Intent intent)
{
Log.i(TAG, "NotifierService: handling an intent");
}


The manifest has these:

<service android:name=".NotifierService" />
<receiver android:name=".Notifier" />


Why doesn't the AlarmManager trigger the receiver?

Answer

Change:

Intent i = new Intent(this, Notifier.class);

To:

Intent i = new Intent(this, NotifierService .class);