2016-01-31 6 views
1

J'essaie d'afficher une notification une seule fois à partir d'une méthode de mise à jour en cours. Il y a une condition qui permettra l'affichage de la notification. Le problème est qu'il continue à afficher des notifications après le premier et le téléphone continue à sonner et à vibrer. Je veux recevoir seulement une notification informant de quelque chose et arrêtez. Est-ce possible? Quelqu'un peut-il aider s'il vous plaît et suggérer des façons alternatives de le faire. Merci.Affichage de la notification une seule fois à partir de la méthode onprogressupdate

Voici mon code pour la méthode onPorgressUpdate:

protected void onProgressUpdate(byte[]... values) { 
     super.onProgressUpdate(values); 
     String command=new String(values[0]);//get the String from the recieved bytes 
     String[] parts= command.split(","); 
     String part1=parts[0]; 
     String part2=parts[1]; 

     temp.setText(part1); 
     humi.setText(part2); 

     if(Integer.parseInt(part2)>70) 
     { 
      NotificationCompat.Builder builder=new NotificationCompat.Builder(this.context); 
      builder.setContentTitle("AM Home Automation"); 
      builder.setContentText("humi"); 
      builder.setSmallIcon(R.drawable.ic_launcher); 
      builder.setTicker("alert"); 
      builder.setDefaults(Notification.DEFAULT_ALL); 

      notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(0, builder.build()); 
     } 

Répondre

0

Utilisez un booléen de se rappeler que vous montriez déjà la notification et l'utiliser pour ne pas montrer une autre personne comme ci-dessous:

private boolean alreadyDisplayedNotification = false; 

protected void onProgressUpdate(byte[]... values) { 
    super.onProgressUpdate(values); 
    String command=new String(values[0]);//get the String from the recieved bytes 
    String[] parts= command.split(","); 
    String part1=parts[0]; 
    String part2=parts[1]; 

    temp.setText(part1); 
    humi.setText(part2); 

    if(Integer.parseInt(part2)>70 && !alreadyDisplayedNotification) { 
     NotificationCompat.Builder builder=new NotificationCompat.Builder(this.context); 
     builder.setContentTitle("AM Home Automation"); 
     builder.setContentText("humi"); 
     builder.setSmallIcon(R.drawable.ic_launcher); 
     builder.setTicker("alert"); 
     builder.setDefaults(Notification.DEFAULT_ALL); 

     notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, builder.build()); 

     alreadyDisplayedNotification=true; 
    } 
+0

merci monsieur, cela a fonctionné: D – akshay270494

+0

@ akshay270494: heureux, je pourrais aider – thedarkpassenger

+0

monsieur, comment ajouter un délai de 20 secondes avant que la boucle if est à nouveau exécuté pour vérifier est-il encore> 70? pourriez-vous s'il vous plaît aider ou suggérer d'autres façons – akshay270494