2016-01-31 27 views
1

我想從onprogress更新方法只顯示一次通知。有條件允許顯示通知。問題是,它繼續在第一個通知之後顯示通知,並且手機持續振鈴和振動。我只想收到一條通知,通知某事並停止。有可能嗎?有人可以幫助,並建議採取替代方法。謝謝。從onprogressupdate方法只顯示一次通知

這裏是我的代碼爲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()); 
     } 

回答

0

使用布爾記住你已經顯示了通知,並用它來沒有表現出一個又一個象下面這樣:

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

謝謝主席先生,它的工作原理:D – akshay270494

+0

@ akshay270494:很高興,我可以幫助 – thedarkpassenger

+0

先生,如何在if循環再次運行之前添加20秒的延遲以檢查它是否仍然> 70?你能否請幫忙或建議其他方式 – akshay270494