Вопрос

I need to create an android app which save events and reminders in a database. When the correct time comes it should show a notification. For that case I need to create a service to look for events for any particular time. What is the method I should use to do such a thing ?

Thanks in Advance !!!

Это было полезно?

Решение

Use AlarmManager to arrange to get control when the first event occurs. At that time, you can do whatever it is you need to do (e.g., display a Notification to remind the user) plus schedule the next alarm for the next event.

Другие советы

What I did was to create a Service and have it do the polling, and when it finds something it then calls an Activity to display the notification.

For the Service I just use a thread that calls itself, and then has a delayed call to a handler.

I went with a service so it could run in the background, as it seems to be better than keeping an activity alive that may be killed off.

private Handler mHandler = new Handler();
public static final int ONE_MINUTE = 60000;
private Runnable periodicTask = new Runnable() {
    public void run() {
        mHandler.postDelayed(periodicTask, TIME_DELAY * ONE_MINUTE);
        /* call your database code here */
    }
};

I had the delay be user-configurable in minutes.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top