Android NotificationListenerService 获取 EXTRA_SMALL_ICON 总是 returns 空

Android NotificationListenerService get EXTRA_SMALL_ICON always returns null

我正在开发一个应用程序来读取 Android 的活动通知。到目前为止,我一直很成功,直到我被 EXTRA_SMALL_ICON 困住了。我使用以下代码来检索应用程序图标和大图标,它们都工作正常。

//工作正常

Drawable appIcon = null;
try {
     appIcon = getPackageManager().getApplicationIcon(packageName);
} catch (PackageManager.NameNotFoundException e) {
     e.printStackTrace();
}

//工作正常

Bitmap largeIcon = null;
try {
    largeIcon = (Bitmap) notification.extras.getParcelable(Notification.EXTRA_LARGE_ICON);
} catch (Exception e) {
    e.printStackTrace();
}

//不工作

Bitmap smallIcon = null;
   try {
        smallIcon = (Bitmap) notification.extras.getParcelable(Notification.EXTRA_SMALL_ICON);
   } catch (Exception e) {
        e.printStackTrace();
   }

获取 SMALL_ICON 引发以下异常。

Key android.icon expected Parcelable but value was a java.lang.Integer.  The default value <null> was returned.
W/Bundle: Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.Integer cannot be cast to android.os.Parcelable

是的,我尝试获取的通知有一个小图标集。我做错什么了吗?还有其他方法可以 gt SMALL_ICON 吗?我不明白为什么。

谢谢!

试试改成这个

Bitmap smallIcon = null;
try {
    int id = notification.extras.getInt(Notification.EXTRA_SMALL_ICON);
} catch (Exception e) {
    e.printStackTrace();
}

之后使用这个

    String pack = sbn.getPackageName(); 
    Context remotePackageContext = null; 
    Bitmap bmp = null;
    try {  
        remotePackageContext = getApplicationContext().createPackageContext(pack, 0);  
        Drawable icon = remotePackageContext.getResources().getDrawable(id);  
        if(icon !=null) {  
            bmp = ((BitmapDrawable) icon).getBitmap();
        }

    } catch (Exception e) {  
        e.printStackTrace();  
    }