0

私はAndroidのアクティブな通知を読むためのアプリを開発しています。これまで私はEXTRA_SMALL_ICONを取得することに固執するまで成功しています。私はアプリのアイコンと大きなアイコンを取得するために次のコードを使用し、両方ともうまく動作します。Android NotificationListenerService get EXTRA_SMALL_ICONは常にnullを返します

//は細かい

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

//はSMALL_ICONは、次の例外をスロー取得

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

を働いていない細かい

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

//ワークス。

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に他の方法がありますか?なぜ私は理解できません。

ありがとうございます!

+0

小さなアイコンが描画可能リソースIDでないビットマップとして渡されている使用後に、この

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

にそれを変更してみてください。通知の作成と同じ方法です。 –

答えて

1

この

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(); 
    } 
+0

intをBitmapにキャストできません。 –

+0

サービスコードを追加することができます –

+0

私は私の答えを更新しました。私はあなたが余分から得たintからビットマップを得ることができると思います –

関連する問題