0

我目前正在研究一個應用程序,它將不斷從Web服務器獲取傳感器數據並將其顯示在文本視圖中。我正在使用Asynctask獲取數據,並且OnprogressUpdate方法會不斷獲取新數據並顯示它。只要傳感器數據大於70,就會發送通知。來自asynctack的通知OnProgressUpdate方法

我遇到的問題是,只有一個通知發送時,數據大於70,當它減少,並重新增加沒有通知發送。

當我刪除布爾值時,我的手機繼續發送通知,這很煩人。當數據大於70時,有沒有辦法發出通知,當數據減少並再次增加時,會發送另一個通知?

請注意,手機將接收2個傳感器數據,它將不斷顯示文本視圖中的數據。

下面是OnProgressMethod的代碼。希望你們能幫我解答。謝謝。

OnProgressUpdate方法:

private boolean alreadyDisplayedNotification = false; 
 

 
protected void onProgressUpdate(byte[]... values) { 
 
\t \t 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(Float.parseFloat(part2)>70.00 && !alreadyDisplayedNotification) 
 
     { 
 
     \t NotificationCompat.Builder builder=new NotificationCompat.Builder(this.context); 
 
    \t \t \t builder.setContentTitle("AM Home Automation"); 
 
    \t \t \t builder.setContentText("humidity is high"); 
 
    \t \t \t builder.setSmallIcon(R.drawable.ic_launcher); 
 
    \t \t \t builder.setTicker("alert"); 
 
    \t \t \t builder.setDefaults(Notification.DEFAULT_ALL); 
 
    \t \t \t //builder.setSound(Uri.parse("android.resource://"+getPackageName()+"/"+R)); 
 
    \t \t \t 
 
    \t \t \t notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
 
    \t \t \t notificationManager.notify(1, builder.build()); 
 
    \t \t \t //notificationID--; 
 
    \t \t \t alreadyDisplayedNotification=true; 
 
    \t \t \t 
 
     } 
 
     
 
      
 
     
 
    }

回答

1

我想:

if(Float.parseFloat(part2)<=70.00){ 
    alreadyDisplayedNotification=false; 
}