2011-09-24 15 views
5

Sé cómo iniciar una notificación X milisegundos después de un evento de clic. Un código como este¿Cómo iniciar la notificación en fecha y hora personalizadas?

 Timer timer = new Timer(); 
     TimerTask timerTask = new TimerTask() { 
      @Override 
      public void run() { 
       triggerNotification(); 
      } 
     }; 
     timer.schedule(timerTask, 3000); 

Cuando el código para la notificación es el siguiente

CharSequence title = "Hello"; 
CharSequence message = "Hello, Android!"; 

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
Notification notification = new Notification(R.drawable.icon, "A New Message!", System.currentTimeMillis()); 

Intent notificationIntent = new Intent(this, AndroidAlarmService.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

notification.setLatestEventInfo(AndroidAlarmService.this, title, message, pendingIntent); 
notification.defaults = Notification.DEFAULT_SOUND; 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 
notificationManager.notify(NOTIFICATION_ID, notification); 

¿Cómo puedo configurar la notificación para que aparezca en la fecha específica en una hora concreta, deja decir 1 de octubre de 19:00?

Respuesta

16

Creo que la mejor manera será crear un servicio que establezca la notificación y luego activar el servicio utilizando un AlarmManager.
Aquí está mi código para hacerlo. Ese es el código de la AlarmManager:

private void startAlarm() { 
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(int year, int month, int date, int hour, int minute, int second); 
    long when = calendar.getTimeInMillis();   // notification time 
      Intent intent = new Intent(this, ReminderService.class); 
      PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0); 
      alarmManager.set(AlarmManager.RTC, when, pendingIntent); 
     } 

Aquí es el servicio:

public class ReminderService extends IntentService { 
    private static final int NOTIF_ID = 1; 

    public ReminderService(){ 
     super("ReminderService"); 
    } 

    @Override 
     protected void onHandleIntent(Intent intent) { 
     NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     long when = System.currentTimeMillis();   // notification time 
     Notification notification = new Notification(R.drawable.icon, "reminder", when); 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.flags |= notification.FLAG_AUTO_CANCEL; 
     Intent notificationIntent = new Intent(this, YourActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0); 
     notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent); 
     nm.notify(NOTIF_ID, notification); 
    } 

} 
+0

y lo que puede ser la estructura del 'when' variable si como' bergnam' dijo a 1 de octubre, 19:00 ? 'long when = 01/10/2011 7 PM;'? – androniennn

+0

@bergnam simplemente necesita usar un objeto Date para establecer la fecha y luego llamar a .getTime() – eladrich

+0

'SimpleDateFormat dateLongue = new SimpleDateFormat (" DD MM aaaa HH: mm: ss ");' y ¿cómo y por qué se llama a .getTime? – androniennn

Cuestiones relacionadas