2011-11-26 106 views
9

我希望我的通知每天在中午12點運行。你如何用時間替換時間值?將通知設置爲特定時間

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
Notification notify = new Notification(R.drawable.icon,"Its Time to Eat",when); 

Context context = GrubNOWActivity.this; 
CharSequence title = "Its Time to Eat"; 
CharSequence details = "Click Here to Search for Restaurants"; 
Intent intent = new Intent(context,Search.class); 
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0); 
notify.setLatestEventInfo(context, title, details, pending); 
nm.notify(0,notify); 

回答

41

您可以使用報警經理

Intent myIntent = new Intent(ThisApp.this , myService.class);  
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0); 

Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 12); 
calendar.set(Calendar.MINUTE, 00); 
calendar.set(Calendar.SECOND, 00); 

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent); //set repeating every 24 hours 

,並把該通知爲myService類裏面!

+0

似乎已經做到了,謝謝! –

+0

但是如何在特定時間設置它?假設我想在10Pm時設置它,我該怎麼做? – silverFoxA

+0

如何將其設置爲特定的一天,例如星期一至星期五,現在是晚上12點? –

1

when參數用於排序狀態欄中的通知。您應該編寫應用程序的代碼,以便在所需的時間觸發通知。