2012-10-29 12 views
8

Chciałbym zrobić aplikację, która pozwoli użytkownikowi zdecydować, co chce mu czas codziennie przypominać coś z powiadomienia ...Powiadomienia w określonym czasie każdego dnia android

Chciałbym wiedzieć, jak mam aby wywołać powiadomienie w czasie secific użytkownik chce ex 7 rano i go dotknij powiadomienia i wprowadź aplikację w określonej działalności. Ale kiedy użytkownik nie chce otrzymywać żadnych powiadomień (za pomocą kliknięcia przycisku), jak mogę anulować wszystkie powiadomienia ...?

Zrobiłem coś podobnego

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

ale nie ma szczęścia do tej pory ....

Dzięki ...

< < >>

Robię to

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

ale nie ma szczęścia ...

proszę o pomoc?

Odpowiedz

24

Zastosowanie alarm manager i umieścić swoje zgłoszenie w NotifyService klasy

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

W jaki sposób mogę użyć warunku do sprawdzenia przed wyświetlaniem codziennie o określonej godzinie, nawet jeśli aplikacja jest zamknięta – Si8

2

wiem, że to jest stare pytanie, ale miałem podobny wymóg i to, co zrobiłem. Znalazłem różnicę między bieżącym czasem systemowym a planowanym czasem wysłania powiadomienia w millis i zaimplementowałem działające w ramach usługi Android. Poniższy kod pojawi się w Notifyservice.

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

Następnie można wywołać usługę z głównej klasy, aby uruchomić ją w tle.

Powiązane problemy