【发布时间】:2016-02-26 21:01:58
【问题描述】:
我目前正在开发一个应用程序,该应用程序将不断地从 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;
}
}
【问题讨论】:
标签: android android-asynctask notifications async-onprogressupdate