Я думаю, что лучшим способом будет создать службу, которая устанавливает уведомление, а затем активировать службу с помощью AlarmManager.
Это код для AlarmManager:
private void startAlarm() {
AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
long when = System.currentTimeMillis(); // notification time
Intent intent = new Intent(this, ReminderService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC, when, (AlarmManager.INTERVAL_FIFTEEN_MINUTES / 3), pendingIntent);
}
Для управления интерваломиспользуйте постоянные значения или просто вставьте свое собственное значение (в миллисекундах).
Вот услуга:
public class ReminderService extends IntentService {
private static final int NOTIF_ID = 1;
public ReminderService(){
super("ReminderService");
}
@Override
protected void onHandleIntent(Intent intent) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long when = System.currentTimeMillis(); // notification time
Notification notification = new Notification(R.drawable.icon, "reminder", when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
nm.notify(NOTIF_ID, notification);
}
}