使用无障碍服务获取通知图标 [英] Get notification icon using an accessibility service

查看:35
本文介绍了使用无障碍服务获取通知图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我目前的代码:

@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 会导致

trying to use this id results in

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

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

推荐答案

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

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;
        }

这篇关于使用无障碍服务获取通知图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆