2

In my situation, I know some methods below

getApplicationContext().getResources().getIdentifier("nameOfDrawable", "drawable", packageName);
getApplicationContext().getPackageManager().getApplicationIcon(packageName);

I want to get app icon's drawable id, but I do not know the "nameOfDrawable" of the app icon. I say I don't know because the default app icon's name can be "ic_launcher.png" or be changed to other like "icon.png". I find the second method above can get the drawable of app icon, but then I don't know what to do. Does anyone know how to get app icon's drawable id? Please help, thanks!

In fact, I want to show a notification, but it need to use the icon's drawable id. I find the first method above can get app icon's drawable id, but it need the name of app icon file which can be different in different app. What I'm doing is to make a little library to do something, so something can not be confirmed.

fisher
  • 123
  • 1
  • 8

2 Answers2

2

This is how you can retrieve and use the Drawable :

Drawable icon = getPackageManager().getApplicationIcon("com.example.samplepackagename");
imageView.setImageDrawable(icon);

The id of any resource is local to that particular application.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • Thanks, I have know that method but how to get id of drawable? I can't find it out:( – fisher Jul 14 '14 at 02:37
  • 1
    Since the id is local only to that particular application, I don't think you'll be able to use it in anyway even if you are able to fetch it. – Shivam Verma Jul 14 '14 at 02:38
  • oh, I also think so, but how to get it? In fact, if I know the name of app icon image file, I can use the first method I had mentioned above to get the id. I have added some description to describe my whole question, how do you think about it? – fisher Jul 14 '14 at 02:49
  • I guess you should check this out : http://stackoverflow.com/questions/15097560/create-a-notification-bar-with-drawable – Shivam Verma Jul 14 '14 at 02:56
1

Finally, I find out the way to solve my problem. The code is below.

String packageName = context.getApplicationContext().getPackageName();
        Intent launchIntent = context.getApplicationContext().getPackageManager().getLaunchIntentForPackage(packageName);
        String className = launchIntent.getComponent().getClassName();

Class<?> c = null;
        if(className != null) {
            try {
                c = Class.forName(className);
            } catch (ClassNotFoundException e) {
                Log.i(TAG, "Throw exception when try to get the main class.");
                e.printStackTrace();
            }
        }

ApplicationInfo info = null;
        try {
            info = context.getApplicationContext().getPackageManager().getApplicationInfo(packageName, 0);
        } catch (NameNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        int icon = info.icon;

long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, null, when);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.defaults = notification.DEFAULT_SOUND|notification.DEFAULT_VIBRATE;
        Intent openintent= new Intent(context, c);
        PendingIntent contentIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, openintent, 0);
        notification.setLatestEventInfo(context.getApplicationContext(), "Notification", message, contentIntent);

        NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFICATION_COUNT, notification);
fisher
  • 123
  • 1
  • 8