2011-11-26 9 views
9

Desidero che la mia notifica venga eseguita ogni giorno alle 12:00. Come si sostituisce il valore when con un tempo?Impostare la notifica sull'ora specifica

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

risposta

41

È possibile utilizzare un gestore di allarme

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 

e mettere la notifica all'interno della classe myService!

+0

che sembra averlo fatto, grazie! –

+0

ma come si imposta per un tempo particolare? diciamo che voglio che venga impostato quando è 10Pm come faccio? – silverFoxA

+0

come impostarlo per un giorno specifico solo per esempio dal lunedì al venerdì e l'ora è 12:00? –

1

Il parametro when è per l'ordinamento delle notifiche nella barra di stato. Dovresti codificare la tua app in modo tale da far scattare la notifica all'ora desiderata.

Problemi correlati