2011-09-24 20 views
5

Bazı tıklama olaylarından sonra bir bildirim X milisaniye nasıl başlatılacağını biliyorum. BuÖzel tarih ve saatte bildirim nasıl başlatılır?

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

gibi bir kodu nereye bildirim için kod Belirli bir zamanda belirli bir tarihte görünmesini bildirimi ayarlamak can, Ekim 1, 7 PM diyelim nasıl bu

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

benziyor?

cevap

16

En iyi yol, bildirimi ayarlayan bir hizmet oluşturmak ve sonra hizmeti bir AlarmManager kullanarak etkinleştirmek olduğunu düşünüyorum.
İşte bunu yapmak için kodum. İşte

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

Servis edilir: O AlarmManager için kod

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

Ve bergnam' 1 Ekim 19:00 dedi 'olarak ne olur' when' değişken yapısı olabilir ? 'uzun olduğunda = 01/10/2011 7 PM; – androniennn

+0

@bergnam sadece tarihi ayarlamanız gereken bir Date nesnesini kullanmalı ve sonra arayalım .getTime() – eladrich

+0

'SimpleDateFormat dateLongue = new SimpleDateFormat (" DD MM yyyy HH: mm: ss ");' ve nasıl ve neden aranır .getTime? – androniennn

İlgili konular