First, you have to create a broadcast receiver that will fired when alarm time changes and phone boots.
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="AlarmBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<receiver android:name="AlertBroadcastReceiver"/>
This broadcast receiver will fire a service for alarm time calculation and will handle actions when an alarm time completes.
public class AlarmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, AlarmService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
context.startForegroundService(serviceIntent);
else
context.startService(serviceIntent);
}
}
In the service class. You should calculate alarm time in milliseconds and set it with Alarm Manager Class. Also you should set a PendingIntent for start an action when alarm time comes.
Intent startIntent = new Intent(getBaseContext(), AlertBroadcastReceiver.class);
startIntent.putExtra("alarm", alarmContent);
startIntent.putExtra("type", "start");
PendingIntent pendingIntentStart = PendingIntent.getBroadcast(getBaseContext(), 0, startIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManagerStart = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManagerStart.set(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
}
else if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
alarmManagerStart.setExact(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(getAlarmTime(alarmArrayNd, "start").getTimeInMillis() >= 900000)
alarmManagerStart.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
else
alarmManagerStart.setExact(AlarmManager.RTC_WAKEUP, getAlarmTime(alarmArray, "start").getTimeInMillis(), pendingIntentStart);
}
In this example, the method getAlarmtime() is a method for calculating alarm time in milliseconds that i created also extras in intent is arbitrary, you can put whatever you want it in.