Mes Mes - 9 months ago 49
Android Question

Scheduled alarm never calls receiver class

I'm trying to set an alarm to fire every 5 minutes.

This is the code for setting the alarm :

public void scheduleAlarmManager() {
Timber.i("After SignIn sets AlarmManager");
// broadcast
Intent intent = new Intent(this, PatientAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this, REQUEST_CODE, intent, 0);

// and set alarmManager
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

Calendar currentCal = Calendar.getInstance();
long currentTIme = currentCal.getTimeInMillis();

// if there's not an Alarm already set then set one
if (!isAlarmSet(this)) {
Timber.i("Alarm not set - so set one");
currentTIme + TWO_MINS_DURATION, TWO_MINS_DURATION, pendingIntent);


and I can verify that I set the alarm correctly since I see in my logcat the messages I log with

My Receiver class is :

public class PatientAlarmReceiver extends BroadcastReceiver {

public static final String TAG = "PATIENT-ALARM-RECEIVER";
public static final String PATIENT_START_ALARM_ACTION = "bp.headsup.receivers.alarm.patient";

public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Inside OnReceive Patient");
Timber.i("Inside OnReceive Patient");

if (intent == null || intent.getAction() == null) {

String action = intent.getAction();
if (PATIENT_START_ALARM_ACTION.equalsIgnoreCase(action)) {

* If is connected to network starts services
private void onStartCheckForConnectionRequest(Context context) {
NetworkUtils networkUtils = new NetworkUtils(context);
if (networkUtils.isNetworkConnected()) {
Intent checkForConnRequestIntent = new Intent(context, PatientCheckForConnectionRequestService.class);
Timber.i("Starts Service From PatientALARMMANAGER");


And I have declared in Manifest :

<!-- Receivers -->
android:name="bp.headsup.receivers.PatientAlarmReceiver" />

Also if I run :
adb shell dumpsys alarm

I can see :

ELAPSED_WAKEUP #0: Alarm{42d804e8 type 2 bp.headsup.mock}
operation=PendingIntent{42d0c230: PendingIntentRecord{42d0f000 bp.headsup.mock broadcastIntent}}

Mock in the above response is the sourceSet I'm using - dont know if it has anything to do with this I just mention it.

The problem is I never read in logcat the messages I have in
on my Receiver class, and obviously no service starts. Anyone can help with that ? I'm using a device which runs with kitKat 4.4 (api 19) but I have tried it with an emulator too and the result was the same.

Answer Source

You're setting an ELAPSED_REALTIME alarm, which is based on the time since the last boot. However, you're passing it a starting time based on the "wall clock", so your alarm is actually set quite far in the future.

You can either change the alarm to an RTC type, or get the starting time from SystemClock.elapsedRealtime(). Given your described behavior, keeping the elapsed type and correcting the starting time seems appropriate.