【问题标题】:Android Notification: How to show sticky/updating alarmAndroid 通知:如何显示粘滞/更新警报
【发布时间】:2020-05-19 10:29:42
【问题描述】:

我有一个应用程序,我想在屏幕顶部显示通知,同时更新时间,并让它一直停留在那里,直到用户关闭它或按下操作。

我的问题是,我如何让通知以这种方式保持在屏幕顶部,并且它是否也可以更新?

【问题讨论】:

    标签: android android-notifications android-alarms


    【解决方案1】:

    要让提醒通知留在那里,您需要使用

    PendingIntent dummyIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    notification.setFullScreenIntent(dummyIntent, true);
    

    我从帖子中得到的理解是,该应用认为它应该将其保留在那里,直到全屏意图采取行动,但它永远不会,因为它是一个虚拟意图。

    https://stackoverflow.com/a/45516069/6296686

    更新通知使用

    notificationBuilder.setOnlyAlertOnce(true);
    
    //Use the same builder when updating
    notificationBuilder.setContentTitle("Updated Title");
    notificationManager.notify(notificationID, notificationBuilder.build());
    

    https://stackoverflow.com/a/15538209/6296686

    【讨论】:

      【解决方案2】:

      编辑:我找到了一个使用通知的实际解决方案。查看其他答案。

      我认为这是通过 AlertDialog 完成的,而不是通知。我个人只是希望它具有“粘性”,但制作更新的自定义 AlertDialog 应该不会太难。为了在屏幕顶部创建一个 AlertDialog,我使用了以下代码。

      AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
      dialogBuilder.setTitle(getNotificationTitle(context, showingTasks));
      dialogBuilder.setNeutralButton("Dismiss", new DialogInterface.OnClickListener(){
                  @Override
                  public void onClick(DialogInterface dialog, int which){
                      //Nothing
                  }
              });
      dialogBuilder.setPositiveButton("Action", new DialogInterface.OnClickListener(){
                  @Override
                  public void onClick(DialogInterface dialog, int which){
                      //Action code
                  }
              });
      Dialog dialog = dialogBuilder.create();
              
      Window window = dialog.getWindow();
      WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
      windowLayoutParams.gravity = Gravity.TOP;
      window.setAttributes(windowLayoutParams);
      
      dialog.show();
      

      https://stackoverflow.com/a/9467151/6296686

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多