2011-08-30 50 views
2

我需要根據從數據庫的日期和時間創建的提醒應用程序(例如:31-08-2011 10:30,05-09-2011 14:40,等等),即使應用程序沒有運行..數據庫將包含許多帶有時間的日期。如果時間到了,我需要顯示通知。我該怎麼做。請提供您應該使用AlarmManager這方面的任何樣品或建議Android的提醒應用

+0

您可以使用報警管理器。 http://developer.android.com/reference/android/app/AlarmManager.html – Samuel

+0

寫的教程。通知提醒:http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ – Blundell

回答

3

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); 

PERIOD是您在OnAlarmReceiver中執行某些事情的時間。 接着,就實施方法

@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); 
} 

而且還看到這裏,

http://developer.android.com/reference/android/app/AlarmManager.html

編輯:A小調代碼問題解決!

+0

什麼的onReceive?它是廣播接收器嗎?你能解釋一下嗎? – Dharmendra

+0

yes,當BroadcastReceiver正在接收Intent廣播時調用此方法。詳細內容點擊這裏http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29 – Randroid

+0

我知道廣播接收器,但我想知道我們如何爲鬧鐘管理員註冊廣播接收機意味着當掛起的意圖觸發時它將如何呼叫? – Dharmendra

-1

把這段代碼,無論你需要..

new CountDownTimer(diff, 1) //Constructor for CountDownTimer class (milliseconds,difference); 
{ 
public void onTick(long millisUntilFinished) 
{ 
// DO NOTHING 
} 

public void onFinish() { 
sendSMS(phoneNo,message); 
} 
}.start(); 
+1

了一下,可能的注意事項,肯定是爲了。 – vonbrand