NotificationListenerService get Notification Icon?

2019-02-15 19:27发布

问题:

In a Service extending the new (SDK18, JB-4.3) NotificationListenerService, I'd like to get the Notification's status bar icon.

mStatusBarNotification.getNotification().icon returns the resource id of the status bar drawable, but that resource id is naturally not within my app's scope/resources. There's also mStatusBarNotification.getNotification().largeIcon (returning a Bitmap), but that's not set for all notifications and returns the "wrong" icon (the image in the expanded notification drawer).

回答1:

Use getPackageName() on StatusBarNotification to find out the app that posted the Notification. You can then use createPackageContext() to get a Context for that package, then use that Context to retrieve the image (e.g., via getResources()).