gpt4 book ai didi

android - 从 onprogressupdate 方法只显示一次通知

转载 作者:行者123 更新时间:2023-12-05 07:51:25 25 4
gpt4 key购买 nike

我试图通过 onprogress 更新方法只显示一次通知。存在允许显示通知的条件。问题是它在第一个通知之后继续显示通知,并且手机一直在响铃和振动。我只想收到一个关于某事的通知并停止。有可能吗?有人可以帮忙并建议其他方法吗?谢谢。

这是我的 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());
}

最佳答案

使用 bool 值来记住您已经显示了通知,并使用它来不显示另一个通知,如下所示:

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;
}

关于android - 从 onprogressupdate 方法只显示一次通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35116350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com