In general, I have a database with dates in the application. I need the application to check these dates a couple of times a day, if the current date is the same as the date in the database, then output a specific notification.

I understand that I need an IntentService and I met him, but I still have no idea how to implement it. There is an idea to create a permanent cycle and check dates and display notifications in it, do I really think? Thank you in advance

    1 answer 1

    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); } } } 
    • so well, I have not met yet with such classes, something seems to be clear. I understand that all I need is to write my code in the onReceice method? - Dimantik02
    • No, you do your code in a separate class-service UpdateService. And AlarmReceiver is easy to wake up on a schedule (schedule) and start your update service. - Andrew Grow