【问题标题】:Get notification icon using an accessibility service使用无障碍服务获取通知图标
【发布时间】:2013-04-27 23:32:46
【问题描述】:

有没有办法获取系统通知的图标? 我可以通过无障碍服务获得通知包裹。我什至可以从中获取图标的 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

【问题讨论】:

    标签: android notifications accessibilityservice


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      看看这是否有效:

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

      【讨论】:

      • 这正是我想要的!感谢您发布此内容
      猜你喜欢
      • 1970-01-01
      • 2014-08-19
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多