使用辅助功能服务获取通知图标

时间:2013-04-27 23:32:46

标签: android notifications accessibilityservice

有没有办法获取系统通知的图标? 我可以通过辅助功能服务获得通知包裹。我甚至可以从中获取图标的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

2 个答案:

答案 0 :(得分: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;
        }

答案 1 :(得分:3)

看看是否有效:

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