我知道在 Android 上,您可以使用 NotificationListenerService 检索当前 Activity 通知的列表。但是,是否可以检索旧通知列表(意味着不再有效)。
我知道 Android 操作系统中有一项名为通知日志的功能。是否可以仅为我的应用程序获取相同的内容?还是必须在应用程序级别处理才能保留这种历史记录?
最佳答案
不幸的是通知日志 uses方法 getHistoricalNotifications
NotificationManagerService
需要 ACCESS_NOTIFICATIONS
权限。因此,它保留给系统应用程序使用:
/**
* System-only API for getting a list of recent (cleared, no longer shown) notifications.
*
* Requires ACCESS_NOTIFICATIONS which is signature|system.
*/
@Override
public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
// enforce() will ensure the calling uid has the correct permission
getContext().enforceCallingOrSelfPermission(
android.Manifest.permission.ACCESS_NOTIFICATIONS,
"NotificationManagerService.getHistoricalNotifications");
StatusBarNotification[] tmp = null;
int uid = Binder.getCallingUid();
// noteOp will check to make sure the callingPkg matches the uid
if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
== AppOpsManager.MODE_ALLOWED) {
synchronized (mArchive) {
tmp = mArchive.getArray(count);
}
}
return tmp;
}
唯一可行的选择是创建一个NotificationListenerService
,实现方法onNotificationPosted
并在本地跟踪应用发布的新通知。
关于Android - 检索旧通知列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36235481/