簡體   English   中英

使用輔助功能服務獲取通知圖標

[英]Get notification icon using an accessibility service

有沒有辦法獲得系統通知的圖標? 我可以通過輔助功能服務獲得通知包裹。 我甚至可以從中獲取圖標的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

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

看看這是否有效:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM