我知道在Android上可以通过NotificationListenerService获取当前活动通知的列表。但是是否可以检索旧通知的列表(即不再活动的通知)。
我知道Android操作系统中有一个名为Notification Log的功能。是否可以仅针对我的应用程序获得类似的内容?还是必须在应用程序级别处理以保留这种历史记录?
我知道在Android上可以通过NotificationListenerService获取当前活动通知的列表。但是是否可以检索旧通知的列表(即不再活动的通知)。
我知道Android操作系统中有一个名为Notification Log的功能。是否可以仅针对我的应用程序获得类似的内容?还是必须在应用程序级别处理以保留这种历史记录?
很遗憾,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
并在本地跟踪应用程序发布的新通知。