仅在onprogressupdate方法中显示通知一次

3
我正在尝试从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());
         }
2个回答

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

@akshay270494:很高兴我能帮到你。 - thedarkpassenger
先生,如何在 if 循环再次运行之前添加 20 秒的延迟来检查它是否仍大于 70?您能帮忙或建议其他方法吗? - akshay270494
你可以在循环结尾加上Thread.sleep(20000),它等同于20秒钟的暂停。 - thedarkpassenger
我添加了一行代码... onprogressupdate停止更新值并继续每20秒发送通知。 - akshay270494
你把这行代码放在哪里了? - thedarkpassenger
在if语句内,在"alreadyDisplayedNotification = true"这一行的后面,用try catch包围起来。 - akshay270494

0

你也可以在通知构建器上使用setOnlyAlertOnce(true),这将仅显示一次悬浮通知

Notification notification = new NotificationCompat.Builder(getActivity().getApplicationContext(),CHANNEL_ID)sensitive content.
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("title")
            .setAutoCancel(false)
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(notiPriority)
            .setOnlyAlertOnce(true) // set this to show and vibrate only once
            .build();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接