9

szukam godzin teraz, jak to zrobić dokładnie:Android Zawiadomienie w czasie

chcę, że codziennie (z wyjątkiem weekendów) powiadomienie jest wysyłane w czasie (powiedzmy 18:00 (= godzina osiemnasta)) z wyjątkiem sytuacji, gdy aplikacja jest już otwarta. Zasadniczo jest to aplikacja Gmaila po otrzymaniu wiadomości e-mail. Gdy użytkownik kliknie na powiadomienie, powinien zniknąć i powinien zostać przeniesiony do głównego centrum danych.

Próbowałem wielu rzeczy z alarmmanager, ale żaden nie spowodował wyświetlenie powiadomienia.

Kod Próbowałem, co czuję jest całkiem blisko jest poprawna, jest następujący: W moim główną działalność:

AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE); 
Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 18); 
Intent intent = new Intent(this, NotificationService.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

Moja NotificationService:

public class NotificationService extends IntentService { 



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

    @Override 
    @SuppressWarnings("deprecation") 
    protected void onHandleIntent(Intent intent) { 
     NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Notification notification = new Notification(R.drawable.ic_launcher, "reminder", System.currentTimeMillis()); 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     Intent notificationIntent = new Intent(this, MainActivity.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(1, notification); 
    } 
} 

Wskazówka używam przestarzałej rzeczy, ponieważ był to nawet jedyny sposób, w jaki powiadomienie pojawiło się, a jednocześnie nie działało z alarmmanager. Jeśli to możliwe, proszę reagować z rozwiązaniem, które nie zawiera już nieaktualnych treści, ale z aktualnymi informacjami: P.

Wiele bardzo wielu z góry dzięki !!!

wyrazami szacunku

Odpowiedz

10

W końcu udało mi się znaleźć rozwiązanie:

private void handleNotification() { 
    Intent alarmIntent = new Intent(this, AlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent); 
} 

To jest mój zwyczaj BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Calendar now = GregorianCalendar.getInstance(); 
     int dayOfWeek = now.get(Calendar.DATE); 
     if(dayOfWeek != 1 && dayOfWeek != 7) { 
      NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle(context.getResources().getString(R.string.message_box_title)) 
        .setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date)); 
      Intent resultIntent = new Intent(context, MainActivity.class); 
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
      stackBuilder.addParentStack(MainActivity.class); 
      stackBuilder.addNextIntent(resultIntent); 
      PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
      mBuilder.setContentIntent(resultPendingIntent); 
      NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      mNotificationManager.notify(1, mBuilder.build()); 
     } 
    } 
} 

I to w manifeście w tagu aplikacji:

<receiver 
     android:name="be.menis.timesheet.service.AlarmReceiver" 
     android:process=":remote" /> 
+0

co to jest 'android: name =" be.menis.timesheet.service.AlarmReceiver "' ?? Chcę skonfigurować powiadomienie o 10:00 codziennie, – Neo

+0

@ashish to jest nazwa pakietu aplikacji i lokalizacja odbiornika –

+0

Dzięki za kod, testuję to teraz. Ale czy ktoś wie, czy ustawię nowy alarm za każdym razem, gdy uruchomię ten kod? jeśli tak, powinienem być z nim bardzo ostrożny ... – George

Powiązane problemy