I am building a scheduling App, I store all scheduled notification inside a Room DB Table, I want to check every second if the current time/date is equal to the DB Table time/date, and it's should display a notification for it.
How can I do this?
I am building a scheduling App, I store all scheduled notification inside a Room DB Table, I want to check every second if the current time/date is equal to the DB Table time/date, and it's should display a notification for it.
How can I do this?
Have a look at this SO question here : Repeat a task with a time delay? have a look at Handler's
postDelayed
feature. get the value from the database you want to check and then use a postDelayed to check if it matches up
I think you should use another design pattern to implement it instead of "check every second", but here is a little snippet how it could work:
final LiveData<Integer> id = scheduledRepository.getScheduledItemId(System.currentTimeMillis());
id.observe(this, new Observer<Integer>() {
@Override
public void onChanged(@Nullable Integer integer) {
if(integer == null){
//do nothing
} else {
Toast.makeText(getApplicationContext(), YOUR_NOTIFICATION_TEXT, Toast.LENGTH_LONG).show();
id.removeObserver(this);
return;
}
}
});
Your DAO will be something like this:
@Query("SELECT id FROM T_SCHEDULER WHERE date = :currentdate")
LiveData<Integer> getScheduledItemId(long currentdate);
Try something like this
Handler handler = new Handler();
int delay = 1000; //milliseconds
handler.postDelayed(new Runnable() {
public void run() { //DB action here
//Todo, here you would put in your DB logic to check if the stuff matches
handler.postDelayed(this, delay);
}
}, delay);