Search code examples
androidalarmmanager

Having trouble registering an alarm with AlarmManager at boot


When a device shuts off, all alarms registered with AlarmManager are wiped out. I am attempting to reschedule my alarm with AlarmManger on boot.

Here is my BroadcastReceiver:

public class DeviceBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "DeviceBootReceiver::onReceive", Toast.LENGTH_LONG).show();

        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Toast.makeText(context,"boot alarm",Toast.LENGTH_LONG).show();

            int intReminderInterval = 0;
            int intDayStart = -1;
            int intDayEnd = -1;

            try {
                Cursor cursor = GulpDb.getReadableDatabase(context).query("users",
                        new String[] {
                                "_id",
                                "day_start",
                                "day_end",
                                "reminder_interval"
                        },
                        null,
                        null,
                        null,
                        null,
                        null);
                if (cursor != null) {
                    if (cursor.getCount() > 0) {
                        cursor.moveToFirst();
                        intReminderInterval = cursor.getInt(cursor.getColumnIndex("reminder_interval"));
                        intDayStart = cursor.getInt(cursor.getColumnIndex("day_start"));
                        intDayEnd = cursor.getInt(cursor.getColumnIndex("day_end"));
                    }
                    cursor.close();
                }
            }
            catch (Exception e) {
                intReminderInterval = 0;
                Log.e("DeviceBootReceiver", "Error: " + e);
            }

            if (intReminderInterval > 0) {
                GulpUtils.createAlarm(context, intDayStart, intDayEnd, intReminderInterval);
            }
        }
    }
}

In my AndroidManifest.xml I have registered my application to receive the BOOT_COMPLETED Intent.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

...

<receiver
    android:name=".DeviceBootReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

However, it appears my alarm is not being registered with AlarmManager.

I know this because the following code returns false:

if (PendingIntent.getBroadcast(this,
        0,
        new Intent(this, AlarmReceiver.class),
        PendingIntent.FLAG_NO_CREATE) != null) {

If you're interested, my createAlarm method looks like this:

    public static void createAlarm(Context context, int intDayStart, int intDayEnd, int intReminderInterval) {
        if (context != null
                && intDayStart >= 0
                && intDayEnd > intDayStart) {
            AlarmManager objAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            PendingIntent intentAlarmIntent = PendingIntent.getBroadcast(context,
                    0,
                    new Intent(context, AlarmReceiver.class),
                    0);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            // If the current time is already past the user's day end setting, create
            // the alarm for tomorrow.
            if (calendar.get(Calendar.HOUR_OF_DAY) >= intDayEnd) {
                calendar.add(Calendar.DAY_OF_YEAR, 1);
            }
            calendar.set(Calendar.HOUR_OF_DAY, intDayStart);
            calendar.set(Calendar.MINUTE, 0);

            objAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(),
                    context.getResources().getInteger(R.integer.minute_in_milliseconds),
//                    context.getResources().getInteger(R.integer.minute_in_milliseconds) * intReminderInterval,
                    intentAlarmIntent);
        }
    }

I can confirm that CreateAlarm works perfectly fine. AlarmReceiver fires in 1 minute intervals. It, obviously, stops working when I shut the device down, and I'm attempting to get it going again.

Any idea what I am doing wrong?


Solution

  • The problem is my <uses-permission> tag was declared after my <application> tag.