来自asynctack OnProgressUpdate方法的通知

时间:2016-02-26 21:01:58

标签: android android-asynctask notifications async-onprogressupdate

我目前正在开发一个应用程序,它将不断从Web服务器获取传感器数据并将其显示在文本视图中。我使用Asynctask获取数据,OnprogressUpdate方法不断获取新数据并显示它。只要传感器数据大于70,就会发送通知。

我遇到的问题是,当数据大于70时,只发送一个通知,当它减少并重新增加时,不会发送任何通知。

当我删除布尔值时,我的手机继续发送通知,这很烦人。有没有办法当数据> 70时,它发送通知,当它减少并再次增加时,会发送另一个通知?

请注意,手机将接收2个传感器数据,并将在文本视图中连续显示数据。

以下是OnProgressMethod的代码。希望你们能帮帮我吧。感谢。

OnProgressUpdate方法:

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

1 个答案:

答案 0 :(得分:1)

我想:

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