1

Possible Duplicate:
How to set Alarm in Android?

I in my application am using Date and Time,But I don't know how set reminder like alarm which will remind 5 min before,10 min before via mail and also on time Popup message display on screen.

Community
  • 1
  • 1
Leena
  • 41
  • 1
  • 4
  • http://developer.android.com/guide/topics/ui/notifiers/notifications.html – pleerock Mar 26 '12 at 11:19
  • Also if you can use content providers you can use content provider of alarm clock of android system. http://developer.android.com/reference/android/provider/AlarmClock.html – pleerock Mar 26 '12 at 11:20

1 Answers1

0

Here is an very easy timer but works great!

Declaration of Handler:

private Handler mHandler = new Handler();

Declaration of Runnable:

private Runnable mUpdateTimeTask = new Runnable() {
           public void run() {
               final long start = mStartTime;
               mStartTime = 0L;
               long millis = SystemClock.uptimeMillis() - start;
               int seconds = (int) (millis / 1000);
               int minutes = seconds / 60;
               seconds     = seconds % 60;

                // do things after 'timerTime' expired.

               mHandler.postAtTime(this,
                       start + (((minutes * 60) + seconds + 1) * 1000));
           }
        };      

Start timer if its not already running:

if (mStartTime == 0L) {
    mStartTime = System.currentTimeMillis();
    mHandler.removeCallbacks(mUpdateTimeTask);
    mHandler.postDelayed(mUpdateTimeTask, timerTime);
}

If you want a reminder even your activity is not in view, you have to put this in a service class. After the Time is expired you can send an eMail in background or fire a notification via PendingIntent.

Fabian Knapp
  • 1,342
  • 13
  • 27