2016-07-20 24 views
0

Ich möchte eine tägliche Benachrichtigung mit Hilfe von AlarmManager anzeigen.Überprüfen, ob AlarmManager vorhanden ist und immer false zurückgibt

Es war richtig funktioniert, bis ich es persistent nach einem Neustart zu machen versucht

Ich schaffe den Alarm wie folgt:

if (switch_notif.isChecked()){ //in my activity's code 
    NotificationReceiver.scheduleAlarms(getApplicationContext()); //allow the notification 

} else { 
    NotificationReceiver.stopNotif(getApplicationContext()); //disable them 
} 

Hier ist meine Rundfunkempfänger:

public class NotificationReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     //Log.d("NotificationReceiver", "onReceive"); 
     this.sendNotif(context); 

     //trying to see if there is already an alarm in place (for reboot) 
     boolean alarmUp = (PendingIntent.getBroadcast(context, 100, 
       new Intent(), 
       PendingIntent.FLAG_NO_CREATE) != null); 
     Log.d("NotificationReceiver", alarmUp? "true":"false"); //always return false 

     if (alarmUp) 
     { 
      //Log.d("NotificationReceiver", "Alarm is already active"); 
     } 
     else { //if no alarm, schedule one 
      //Log.d("NotificationReceiver", "Alarm is not active"); 
      this.scheduleAlarms(context); 

     } 
    } 

    public static void scheduleAlarms(Context context) { 

     //Log.d("NotificationReceiver", "scheduleAlarms"); 
     Calendar alarmStartTime = Calendar.getInstance(); 
     Calendar now = Calendar.getInstance(); 
     alarmStartTime.set(Calendar.HOUR_OF_DAY, 13); 
     alarmStartTime.set(Calendar.MINUTE, 31); 
     alarmStartTime.set(Calendar.SECOND, 30); 
     /*if (now.after(alarmStartTime)) { // not needed when using 1min repeat for testing 
      Log.d("Alarm","Added a day"); 
      alarmStartTime.add(Calendar.DATE, 1); 
     }*/ 

     Intent intent = new Intent(context, NotificationReceiver.class); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,alarmStartTime.getTimeInMillis(),60000 ,pendingIntent); // set to 1minute for testing purpose 

    } 

    public static void sendNotif(Context context){ //fire the notification 

     //Log.d("NotificationReceiver", "sendNotif"); 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(
       Context.NOTIFICATION_SERVICE); 

     Intent intent1 = new Intent(context, Notification.class); 
     intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(
       context, 100, intent1, PendingIntent.FLAG_UPDATE_CURRENT); 

     Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
       .setContentIntent(pendingIntent) 
       .setSmallIcon(R.drawable.ic_notif) 
       .setContentTitle("Rappel MyCardioPad") 
       .setContentText("Vous avez une séance d'éffort aujourd'hui") 
       .setSound(soundUri) 
       .setAutoCancel(true); 

     notificationManager.notify(100, builder.build()); 

    } 

    public static void stopNotif(Context context){ //Allow to turn off the alarm/notification 

     //Log.d("NotificationReceiver", "stopNotif"); 
     Intent intent = new Intent(context, NotificationReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
     alarmManager.cancel(pendingIntent); 
     pendingIntent.cancel(); 
    } 
} 

bearbeiten: Dies ist das Protokoll, wenn ich den Alarm ausschalte und einschalte:

07-20 14:22:58.637 mycardiopad D/NotificationReceiver: scheduleAlarms 
07-20 14:22:59.856 mycardiopad D/NotificationReceiver: onReceive 
07-20 14:22:59.856 mycardiopad D/NotificationReceiver: sendNotif 
07-20 14:22:59.863 mycardiopad D/NotificationReceiver: false 
07-20 14:22:59.863 mycardiopad D/NotificationReceiver: Alarm is not active 
07-20 14:22:59.863 mycardiopad D/NotificationReceiver: scheduleAlarms 
07-20 14:22:59.908 mycardiopad D/NotificationReceiver: onReceive 
07-20 14:22:59.908 mycardiopad D/NotificationReceiver: sendNotif 
07-20 14:22:59.912 mycardiopad D/NotificationReceiver: false 
07-20 14:22:59.912 mycardiopad D/NotificationReceiver: Alarm is not active 
07-20 14:22:59.912 mycardiopad D/NotificationReceiver: scheduleAlarms 
07-20 14:23:00.509 mycardiopad D/NotificationReceiver: onReceive 
07-20 14:23:00.509 mycardiopad D/NotificationReceiver: sendNotif 
07-20 14:23:00.520 mycardiopad D/NotificationReceiver: false 
07-20 14:23:00.520 mycardiopad D/NotificationReceiver: Alarm is not active 

Antwort

1

Vielleicht ist alarmUp immer falsch, weil Sie ein anderes Intent sind mit ihnen zu vergleichen

prüfen, wenn sie vorhanden ist:

boolean alarmUp = (PendingIntent.getBroadcast(context, 100, new Intent(), PendingIntent.FLAG_NO_CREATE) != null); 

den Alarm erstellen:

Intent intent1 = new Intent(context, Notification.class); 
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

Wie Sie kann sehen, Intent Verwendung in beiden Statement sind unterschiedlich. Versuchen Sie, so unter Kontrolle:

Intent tempIntent = new Intent(context, Notification.class); 
tempIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
boolean alarmUp = (PendingIntent.getBroadcast(context, 100, tempIntent, PendingIntent.FLAG_NO_CREATE) != null); 

Durch die Art und Weise

Alle Alarme zerstört werden bei einem Neustart ... Also, sollten Sie sich registrieren Ihre BroadcastReceiverBOOT_COMPLETED Ereignisse zu empfangen. Auf diese Weise wissen Sie, wenn Sie das Ereignis BOOT_COMPLETED erhalten, dass Ihr Gerät neu gestartet wurde und kein Alarm aktiv ist ... Dann werden Sie es erneut eingeben.

+0

Danke das war es! Ich hatte den BroadcastReceiver korrekt registriert, danke, nur die Überprüfung durcheinander gebracht. Ich schrieb auch "Notification.class" anstelle von "NotificationReceiver.class" in sendNotif, aber es war immer noch so (oder war das die Änderung, die Sie vorgeschlagen? Ich ändere etwas, aber nicht das und es funktioniert jetzt) ​​ Ich bin immer noch nicht sicher, wie es funktioniert, ich dachte, es wäre mit der ID (100 hier) – Serk