gpt4 book ai didi

android - 有没有办法让通知保持 Activity 状态,直到用户点击清除?

转载 作者:行者123 更新时间:2023-11-29 17:54:58 25 4
gpt4 key购买 nike

我有一项在后台运行的服务,如果用户有新的 PDF 文件要查看,它会通知用户。当他们单击通知时,会打开一个窗口,让他们可以选择下载 PDF。问题是,一旦用户关闭此窗口,他们将无法返回到该屏幕,因为当他们单击它时,通知已被清除。我希望此通知保持 Activity 状态,直到用户在不小心关闭通知打开的窗口时单击“清除通知”为止。*

*我没有在应用程序中创建永久可访问窗口/ View 的原因是通知对时间敏感。 20 分钟后,PDF 可供他们随时查看。创建另一个永久 View 是多余的。此通知仅用于允许用户更早查看 PDF。

编辑:根据@Rahul Gupta 提供的答案,我发现如果我没有指定标志,我的应用程序正在使用“FLAG_AUTO_CANCEL”。所以对于我的解决方案,我只是放入了另一个标志。因为我使用的是 Titanium Appcelerator,所以我没有 setAutoCancel() 函数,所以这不是我的选择。

最佳答案

API 11以下可以设置Notification.FLAG_NO_CLEAR。这可以像这样实现:

// Create notification
Notification note = new Notification(R.drawable.your_icon, "Example ", 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 支持库时,可以这样实现:

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

或者您可以使用 NotificationCompat 类。

public NotificationCompat.Builder setAutoCancel (boolean autoCancel)

设置此标志将使用户在面板中单击它时自动取消通知。通过setDeleteIntent(PendingIntent)设置的PendingIntent会在通知取消时广播。

关于android - 有没有办法让通知保持 Activity 状态,直到用户点击清除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20054621/

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