2013-04-27 72 views
3

有什麼方法可以獲取系統通知的圖標嗎? 我可以通過無障礙服務獲取通知包裹。我甚至可以從它得到圖標的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

回答

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

見,如果這個工程:

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

+0

這正是我一直在尋找!感謝您發佈此代碼 – adefran83 2013-08-14 16:35:31

+0

代碼中的mIcon是什麼? – Denny 2017-02-06 20:15:01