其他分享
首页 > 其他分享> > android – 使用辅助功能服务获取通知图标

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

作者:互联网

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

解决方法:

所以我设法通过在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;
        }

标签:android,notifications,accessibilityservice
来源: https://codeday.me/bug/20190729/1567816.html