1

In my app, I've got a BroadcastReceiver, which is called by an AlarmManager. That BroadcastReceiver calls CommonsWare's WakefulIntentservice.

I tested this on my phone, and it appeared that sometimes, my BroadcastReceiver isn't called at all. I'm really confused about what it could be. My BroadcastReceiver and WakefulIntentservice are registerd in the manifest.

This is my code: In AlarmActivity:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 2);

Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), savedIntervalAutomaticMilisInt, pendingIntent);

Toast.makeText(this, "Saved", Toast.LENGTH_LONG).show();

finish();

AlarmReceiver:

package com.something.app;

import com.commonsware.cwac.wakeful.WakefulIntentService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, AlarmService.class);
        WakefulIntentService.sendWakefulWork(context, i);
    }
}

And AlarmService:

package com.something.app;

import android.app.PendingIntent;
import android.content.Intent;
import com.commonsware.cwac.wakeful.WakefulIntentService;

public class AlarmService extends WakefulIntentService {

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

    @Override
    protected void doWakefulWork(Intent arg0) {
        //A looooooooot of stuff
    }

Does anyone know why the BroadcastReceiver sometimes isn't called?

EDIT: I heard about setting a BroadcastReceiver which receives onBootCompleted. Is that required?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Xander
  • 5,487
  • 14
  • 49
  • 77
  • That works, I'm pretty sure about that. It is selected by a spinner. – Xander Nov 02 '12 at 16:23
  • Use `adb shell dumpsys alarm` to debug why your alarm is not working. When you run into a case where the broadcast seems like it was not sent, use `adb shell dumpsys alarm` to find out if your alarm is scheduled and when it is scheduled for. You might also consider using logging statements or breakpoints to better detect when the broadcast is sent. "Is that required?" -- only if you need to re-establish your alarm schedule after a device reboot. – CommonsWare Nov 02 '12 at 16:46
  • @CommonsWare Oké, I'll try that. How do I now if it's required to re-establish my alarm after reboot? – Xander Nov 02 '12 at 16:55
  • 1
    "How do I now if it's required to re-establish my alarm after reboot?" -- you hopefully know whether or not you want your alarms to survive a reboot or not. It is your app, after all. When a device is rebooted, all scheduled events in `AlarmManager` are wiped out. So, if you set up an event in `AlarmManager`, and you want that event to remain in `AlarmManager` even after a reboot, you need to do that yourself, typically via an `ACTION_BOOT_COMPLETED` `BroadcastReceiver.` – CommonsWare Nov 02 '12 at 17:00
  • Oh, ok, I thought you meant that it's required if I want to change my alarm on boot. But this could really well be the problem. I'll try this. – Xander Nov 02 '12 at 17:06

1 Answers1

1

So, that's the problem: If the device reboots, it sometimes clears the alarm, so you have to reset them in a BroadcastReceiver which receives onBootCompleted

Xander
  • 5,487
  • 14
  • 49
  • 77