【问题标题】:non removable notification不可移除的通知
【发布时间】:2012-11-17 02:53:38
【问题描述】:

在我的应用程序中,有服务在后台运行。我想通知用户该服务正在运行。但我需要用户不能删除通知 - 通过按清除按钮或在通知栏中将其滑出

这意味着我需要在通知区域上方显示我的通知

【问题讨论】:

    标签: android notifications android-notification-bar


    【解决方案1】:

    这是可能的,但您实现它的方式取决于您开发的 API 级别。

    对于低于 11 的 API 级别,您可以设置Notification.FLAG_NO_CLEAR。这可以这样实现:

    // Create notification
    Notification note = new Notification(R.drawable.your_icon, "Example notification", System.currentTimeMillis());
    
    // Set notification message
    note.setLatestEventInfo(context, "Some text", "Some more text", clickIntent);
    
    // THIS LINE IS THE IMPORTANT ONE            
    // This notification will not be cleared by swiping or by pressing "Clear all"
    note.flags |= Notification.FLAG_NO_CLEAR;
    

    对于 11 以上的 API 级别,或者使用 Android Support Library 时,可以这样实现:

    Notification noti = new Notification.Builder(mContext)
        .setContentTitle("Notification title")
        .setContentText("Notification content")
        .setSmallIcon(R.drawable.yourIcon)
        .setLargeIcon(R.drawable.yourBigIcon)
        .setOngoing(true) // Again, THIS is the important line
        .build();
    

    【讨论】:

    • 结合 antew 的帖子,我正在寻找 Notification.FLAG_ONGOING_EVENT。非常感谢
    【解决方案2】:

    要创建不可移除的通知,只需使用 setOngoing (true);

    NotificationCompat.Builder mBuilder =
                             new NotificationCompat.Builder(this)
    
                            .setSmallIcon(R.drawable.ic_service_launcher)
    
                            .setContentTitle("My title")
    
                            .setOngoing(true)  
    
                            .setContentText("Small text with details");
    

    【讨论】:

      【解决方案3】:

      听起来像 Notification.FLAG_NO_CLEARNotification.FLAG_ONGOING_EVENT 就是您要找的。​​p>

      【讨论】:

        【解决方案4】:

        要创建不可移除的通知,只需使用 setOngoing (true);

        要创建可移动通知,只需使用 setOngoing (false);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-03-17
          • 1970-01-01
          • 1970-01-01
          • 2021-02-03
          • 1970-01-01
          • 1970-01-01
          • 2012-06-28
          相关资源
          最近更新 更多