2012-10-29 16 views
8

मैं एक ऐप्लिकेशन है जो उपयोगकर्ता यह तय करें कि समय हर रोज उसे अधिसूचना के साथ कुछ याद दिलाना चाहता है चलो करना चाहते हैं ...सूचनाएं हर दिन एंड्रॉयड

मैं चाहूँगा पता है कि कैसे मैं माना जाता हूँ विशिष्ट समय में अधिसूचना को ट्रिगर करने के लिए उपयोगकर्ता 7:00 बजे से पहले चाहता है और उसे अधिसूचना पर टैप कर सकता है और विशिष्ट गतिविधि में आवेदन दर्ज कर सकता है। लेकिन जब उपयोगकर्ता कोई और अधिसूचनाएं प्राप्त नहीं करना चाहता (एक बटन क्लिक के साथ) मैं सभी अधिसूचनाओं को कैसे रद्द कर सकता हूं ...?

मैं अब तक की तरह

Intent intent = new Intent(this, main.class); 
Bundle bundle = new Bundle(); 
bundle.putString("title", "Some Title");\ 
intent.putExtras(bundle); 
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 
String body = "Tap to see the Activity"; 
String title = "Title of notification"; 
Notification n = new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis()); 
n.setLatestEventInfo(this, title, body, pi); 
n.defaults = Notification.DEFAULT_ALL; 
nm.notify(uniqueID, n); 

कुछ है, लेकिन कोई किस्मत बना दिया ....

धन्यवाद ...

< < >>

मैं इस

कर रहा हूँ
 String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.notification_icon; 
     CharSequence tickerText = "Ome TickerText"; 
     long when = System.currentTimeMillis(); 
     Context context = getApplicationContext(); 
     CharSequence contentTitle = "Some Title"; 
     CharSequence contentText = "Some Text"; 

     Intent notificationIntent = new Intent(context, LandingActivity.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("title", "a title"); 
     bundle.putString("title2", "title number 2"); 
     bundle.putString("action", "tip"); 
     bundle.putString("greek", "somehting else"); 
     bundle.putInt("action_num", 2); 
     notificationIntent.putExtras(bundle); 
     alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
     contentIntent = PendingIntent.getActivity(Notifications.this, 0, notificationIntent, 0); 

     Notification notification = new Notification(icon, tickerText, when); 
     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
     notification.defaults = Notification.DEFAULT_ALL; 

     mNotificationManager.notify(UniqueID, notification); 

     int hour = tp.getCurrentHour(); 
     int minutes = tp.getCurrentMinute(); 

     contentIntent = PendingIntent.getService(Notifications.this, 0, notificationIntent, 0); 
     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.HOUR_OF_DAY, hour); 
     calendar.set(Calendar.MINUTE, minutes); 
     calendar.set(Calendar.SECOND, 00); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent); 

लेकिन कोई भाग्य नहीं ...

कोई मदद कृपया?

उत्तर

24

उपयोग alarm manager और NotifyService वर्ग

Intent myIntent = new Intent(Current.this , NotifyService.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(), AlarmManager.INTERVAL_DAY , pendingIntent); //set repeating every 24 hours 
+1

मैं कुछ समय पर दैनिक प्रदर्शित करने से पहले जांच करने के लिए स्थिति का उपयोग कैसे कर सकता हूं, भले ही ऐप बंद हो – Si8

2

में अपनी अधिसूचना डाल मैं जानता हूँ कि यह एक पुराने सवाल है, लेकिन मैं एक ऐसी ही आवश्यकता थी और यह मैं क्या किया है। मुझे मौजूदा सिस्टम समय और मिलिस में अधिसूचना भेजने के लिए इच्छित समय के बीच का अंतर मिला और एंड्रॉइड सेवा के अंदर एक रननेबल लागू किया गया। नीचे कोड नोटिफ़िस सेवा में आएगा।

private Handler notificationTimer = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     if(msg.what==0) { 
       sendNotifications(); 
     } 
     super.handleMessage(msg); 
    } 
}; 
private Runnable notificationCaller = new Runnable() { 

    @Override 
    public void run() { 
     Message msg = notificationTimer.obtainMessage(); 
     msg.what = 0; 
     notificationTimer.sendMessage(msg); 
    } 
};   


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

    Long difference = calendar.getTimeInMillis() - timefornotification.getTimeInMillis(); 

    if(difference<0){ 
     notificationTimer.postDelayed(notificationCaller, -(difference)); 
    } 
    else{ 
     notificationTimer.postDelayed(notificationCaller, difference); 
    } 

    public void sendNotifications(){ 
     ------Your code here------- 
     notificationTimer.removeCallbacksAndMessages(null); 
     notificationTimer.postDelayed(notificationCaller, 86400000); 
     ------86400000 is one day. So it will repeat everyday------- 
    } 

फिर आप पृष्ठभूमि में इसे चलाने के लिए मुख्य कक्षा से सेवा कॉल कर सकते हैं।

Intent service = new Intent(this, NotifyService.class); 
    this.startService(service); 
संबंधित मुद्दे