Xander
Xander

Reputation: 5587

Android: AlarmManager sometimes doesn't call BroadcastReceiver

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?

Upvotes: 1

Views: 1584

Answers (1)

Xander
Xander

Reputation: 5587

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

Upvotes: 1

Related Questions