2

That's how I create alarm

    PendingIntent alarmIntent = PendingIntent.getBroadcast(getContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarm = (AlarmManager) getContext().getSystemService(ALARM_SERVICE);

    Calendar startTime = Calendar.getInstance();

    startTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
    startTime.set(Calendar.MINUTE, minute);
    startTime.set(Calendar.SECOND, 0);

    if (startTime.getTimeInMillis() < System.currentTimeMillis()) {
        startTime.add(Calendar.DAY_OF_MONTH, 1);
    }

    long intendedTime = startTime.getTimeInMillis();

    alarm.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, alarmIntent);

Method that I call in BroadcastReceiver:

private void startAlarm(Context context) {
    Intent mainIntent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 1, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager myNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context);
    builder.setSmallIcon(R.drawable.l_active)
            .setContentTitle(context.getString(R.string.morn_title))
            .setContentText(context.getString(R.string.morn_text))
            .setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent);
    myNotificationManager.notify(1, builder.build());
}

So I had a problem like here . When I was setting alarm to 9:51 and my time was 10:00 - alarm wasn't working. After adding

if (startTime.getTimeInMillis() < System.currentTimeMillis()) {
        startTime.add(Calendar.DAY_OF_MONTH, 1);
}

Alarm works fine, but when I reboot phone - it doesn't work. I will try to explain with an example: Time on my phone is 23:50. I set alarm on 00:05, then I reboot the phone and wait until 00:05. And alarm doesn't fire. But! If I won't reboot my phone - everything works. And one more thing: I don't have problems with phone reboot when I do not set time on the past (for example, my time is 22:00, I set time to 22:05, reboot phone and it works)

D. Joe
  • 75
  • 1
  • 8

1 Answers1

4

AlarmManager doesn't persist even after reboot. You make AlarmManager work after reboot by creating a BroadCastReceiver which will start the Alarm while booting completes of the device.

Use <action android:name="android.intent.action.BOOT_COMPLETED" /> for trapping boot activity in BroadCastReceiver class.

You need to add above line in AndroidManifest.xml as follows :

<receiver android:name=".StartUpReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
</receiver>

class StartUpReceiver

public class StartUpReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
  
      if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
         // reset your alarms here 
      }
  
  }
}  

In StartUpReceiver you can create alarm by AlarmManager again. I suggest use need to store alarm info in database or SharedPreferences so will have info to create alarm when device finish reboot.

Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39
Tung Duong
  • 1,156
  • 7
  • 19