I need to create reminder application based on the date and time from the database (Eg:31-08-2011 10:30,05-09-2011 14:40,etc.. )even if the app is not runn开发者_如何转开发ing.. Database will contain many dates with times. If the time reaches i need to display the notificaiton.How can I do that. Please provide any samples or suggestions
You should use AlarmManager for this.
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);
}
And also see here,
http://developer.android.com/reference/android/app/AlarmManager.html
Edit: A minor code issue fixed!
Put this code wherever you need..
new CountDownTimer(diff, 1) //Constructor for CountDownTimer class (milliseconds,difference);
{
public void onTick(long millisUntilFinished)
{
// DO NOTHING
}
public void onFinish() {
sendSMS(phoneNo,message);
}
}.start();
精彩评论