Issue
I'm developing an app to read active notifications of Android. So far I have being successful until I got stucked with getting EXTRA_SMALL_ICON. I use following piece of code to retrive app icon and large icon and they both work just fine.
//Works fine
Drawable appIcon = null;
try {
appIcon = getPackageManager().getApplicationIcon(packageName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
//Works fine
Bitmap largeIcon = null;
try {
largeIcon = (Bitmap) notification.extras.getParcelable(Notification.EXTRA_LARGE_ICON);
} catch (Exception e) {
e.printStackTrace();
}
//NOT WORKING
Bitmap smallIcon = null;
try {
smallIcon = (Bitmap) notification.extras.getParcelable(Notification.EXTRA_SMALL_ICON);
} catch (Exception e) {
e.printStackTrace();
}
Getting SMALL_ICON throws following exception.
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
And yes, the notification I'm trying to get has a small icon set. Am I doing anything wrong or is there any other way to gt SMALL_ICON? I can not figure out why.
Thanks!
Solution
try changing it to this
Bitmap smallIcon = null;
try {
int id = notification.extras.getInt(Notification.EXTRA_SMALL_ICON);
} catch (Exception e) {
e.printStackTrace();
}
after that use this
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();
}
Answered By - masoud vali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.