Android - 获取旧通知列表

6

我知道在Android上可以通过NotificationListenerService获取当前活动通知的列表。但是是否可以检索旧通知的列表(即不再活动的通知)。

我知道Android操作系统中有一个名为Notification Log的功能。是否可以仅针对我的应用程序获得类似的内容?还是必须在应用程序级别处理以保留这种历史记录?

1个回答

5

很遗憾,Notification Log 使用 NotificationManagerService 的方法getHistoricalNotifications,需要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并在本地跟踪应用程序发布的新通知。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接