I make reminder in my app. User can set time for reminder. How to make eternal service, who will check device time and if it equal user set time - output the notification. I try like this, but it work few times and die. What have I done wrong?
void redinder2() {
for (int i = 1; i<=10; i++) {
Calendar calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
if(hour==my_hour&&minute==my_minyte){){ sendNotif();}
if(i==10){startService(new Intent(this, MyService.class));}
try {
TimeUnit.SECONDS.sleep(59);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
You cannot make an "eternal service", nor would that be the correct approach for this problem. Only have a service running when it is actively delivering value to the user. Watching the clock tick is not actively delivering value to the user.
Use AlarmManager
to arrange to get control at the time to do the reminder.