【问题标题】:displaying notification only once from onprogressupdate method从 onprogressupdate 方法只显示一次通知
【发布时间】:2016-01-31 16:42:13
【问题描述】:

我正在尝试通过 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());
         }

【问题讨论】:

    标签: android notifications async-onprogressupdate


    【解决方案1】:

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

    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:很高兴,我可以帮忙
    • 先生,如何在 if 循环再次运行之前添加 20 秒的延迟以检查它是否仍然 > 70 ?你能帮忙或建议其他方法吗
    • 你可以把 Thread.sleep(20000) 等于 20 秒的时间放在循环的末尾..
    • 我添加了这一行...... onprogressupdate 停止更新值并继续每 20 秒发送一次通知
    • 你把这行放在哪里了?
    【解决方案2】:

    您也可以在通知生成器上使用 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2017-09-22
      相关资源
      最近更新 更多