2011-09-24 9 views
5

मुझे पता है कि कुछ क्लिक ईवेंट के बाद अधिसूचना एक्स मिलीसेकंड कैसे शुरू करें। इसकस्टम दिनांक और समय पर अधिसूचना कैसे शुरू करें?

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

की तरह एक कोड अधिसूचना के लिए कोड जहां इस

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

मैं कैसे विशिष्ट समय पर विशिष्ट तिथि पर प्रकट करने के लिए सूचना सेट कर सकते हैं, 1 अक्टूबर 7 बजे मान लीजिए की तरह दिखता है?

उत्तर

16

मुझे लगता है कि अधिसूचना सेट करने वाली सेवा बनाने का सबसे अच्छा तरीका होगा और फिर 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); 
     } 

यहाँ है सेवा:: कि AlarmManager के लिए कोड है

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

और अगर के रूप में 'bergnam' 1 अक्टूबर को शाम 7 बजे कहा क्या' when' चर की संरचना हो सकता है ? 'लंबा कब = 01/10/2011 7 अपराह्न ;'? – androniennn

+0

@bergnam बस बस तारीख को सेट करने के लिए डेट ऑब्जेक्ट का उपयोग करने की आवश्यकता है और फिर कॉल करें .getTime() – eladrich

+0

'SimpleDateFormat dateLongue = new SimpleDateFormat ("डीडी एमएम yyyy एचएच: मिमी: एसएस");' और कैसे और क्यों कॉलिंग .getTime? – androniennn

संबंधित मुद्दे