Is there any way to get icons of system notifications? I can get a notification parcel with Accessibility service. I can even get the id of the icon from it, but I am stuck there. I have no idea how to use the id to get the actual bitmap so I can display it, because it is not from my apk.
Here is my code so far:
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d("onAccessibilityEvent");
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
if (event.getParcelableData() instanceof Notification) {
Notification notification = (Notification) event.getParcelableData();
Log.d("ticker: " + notification.tickerText);
Log.d("icon: " + notification.icon);
Log.d("largeIcon: " + notification.largeIcon);
}
Log.d("notification: " + event.getText());
}
}
trying to use this id results in
android.content.res.Resources$NotFoundException: Resource ID #0x7f0200ad
So I managed to accomplish this by searching for the first ImageView in the RemoteViews
extractImage(notification.contentView);
...
private void extractImage(RemoteViews views) {
LinearLayout ll = new LinearLayout(getApplicationContext());
View view = views.apply(getApplicationContext(), ll);
drawable = searchForBitmap(view);
Log.d("Drawable: " + drawable);
}
private Drawable searchForBitmap(View view) {
if (view instanceof ImageView) {
return ((ImageView) view).getDrawable();
}
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
Drawable result = searchForBitmap(viewGroup.getChildAt(i));
if (result != null) {
return result;
}
}
}
return null;
}
See if this works:
String mPackageName = event.getPackageName();
Context remotePackageContext = context.createPackageContext(mPackageName, 0);
Drawable icon = remotePackageContext.getResources().getDrawable(mIcon);
来源:https://stackoverflow.com/questions/16257986/get-notification-icon-using-an-accessibility-service