【问题标题】:Android - retrieve list of old notificationsAndroid - 检索旧通知列表
【发布时间】:2016-03-26 12:49:05
【问题描述】:
我知道在 Android 上,您可以使用 NotificationListenerService 检索当前活动通知的列表。但是,是否可以检索旧通知列表(意味着不再活动)。
我知道 Android 操作系统中有一个名为 Notification Log 的功能。是否可以仅为我的应用程序获取相同的内容?还是必须在应用程序级别进行处理才能保留这种历史记录?
【问题讨论】:
标签:
android
android-notifications
【解决方案1】:
不幸的是,通知日志uses 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 方法并在本地跟踪应用发布的新通知。