3

有没有办法获取系统通知的图标?我可以通过无障碍服务获得通知包裹。我什至可以从中获取图标的 id,但我被困在那里。我不知道如何使用 id 来获取实际的位图以便我可以显示它,因为它不是来自我的 apk。

到目前为止,这是我的代码:

@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());
        }
    }

尝试使用此 ID 会导致

android.content.res.Resources$NotFoundException:资源 ID #0x7f0200ad

4

2 回答 2

4

所以我设法通过在 RemoteViews 中搜索第一个 ImageView 来实现这一点

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;
        }
于 2013-04-27T23:55:32.820 回答
3

看看这是否有效:

String mPackageName = event.getPackageName();
Context remotePackageContext = context.createPackageContext(mPackageName, 0);
Drawable icon = remotePackageContext.getResources().getDrawable(mIcon);

于 2013-08-08T20:51:02.790 回答