M.V.
M.V.

Reputation: 1672

Android reminder!

I would like to ask which service and how to use to make reminder in android... Let's say: after 10 minutes from now show me notification in notification bar...

Thanks for your answer

Upvotes: 3

Views: 4028

Answers (3)

Martijn Lenderink
Martijn Lenderink

Reputation: 535

Somethink like this i guess, you start an runnable after 10min and open an notification in the runnable's code.

Runnable reminder = new Runnable()
{
    public void run()
    {
        int NOTIFICATION_ID = 1324;
        NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification note = new Notification(R.drawable.icon, "message", System.currentTimeMillis());

        // Add and AUTO_CANCEL flag to the notification, 
        // this automatically removes the notification when the user presses it
        note.flags |= Notification.FLAG_AUTO_CANCEL;    

        PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, ActivityToOpen.class), 0);

        note.setLatestEventInfo(this, "message", title, i);
        notifManager.notify(NOTIFICATION_ID, note);
    }
};

Handler handler = new Handler();
handler.postDelayed(reminder, 600000);

Upvotes: 1

acm0x
acm0x

Reputation: 775

Obviously you should use AlarmManager in order to setup something to be executed in given PERIOD.

AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, OnAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
SystemClock.elapsedRealtime(), PERIOD, pi);

where the PERIOD is your time to something that should be executed in OnAlarmReceiver. And then, just implement method in

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager nm = (NotificationManager);
    context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification();
    notification.tickerText = "10 Minutes past";
    nm.notify(0, notification);
}

Enjoy.

Upvotes: 9

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43088

You should use AlarmManager. With it you can schedule an intent to be delivered. Create a BroadcastReceiver to get it and show the notification.

Upvotes: 2

Related Questions