You need a separate AlarmReceiver - it puts itself in an endless schedule to start the service / action we need. In our case, it infinitely launches UpdateService. Please note that in order for the service not to be killed in 6-7 Android, you need to restart using the setExactAndAllowWhileIdle () method.
Here is a working example of such a receiver:
public class AlarmReceiver extends WakefulBroadcastReceiver { public static final String INTENT_ALARM_UPDATE = "com.example.ALARM_UPDATE"; @Override public void onReceive(final Context context, Intent intent) { Log.d(Constants.LOG, "Сработал таймер для запуска сервиса обновления"); UpdateService.start(context, intent); setResultCode(Activity.RESULT_OK); // Next alarm schedule(context); } public static void schedule(Context context) { Calendar calUpdater = Calendar.getInstance(); calUpdater.add(Calendar.MILLISECOND, Constants.TIME_UPDATE); AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intentAlarm = new Intent(INTENT_ALARM_UPDATE); PendingIntent pending = PendingIntent.getBroadcast(context, 0, intentAlarm, PendingIntent.FLAG_CANCEL_CURRENT); if (Build.VERSION.SDK_INT>Build.VERSION_CODES.M) { service.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calUpdater.getTimeInMillis(), pending); } else { service.set(AlarmManager.RTC_WAKEUP, calUpdater.getTimeInMillis(), pending); } } }