如何获取带活动名称的活动图标

时间:2015-02-12 13:40:11

标签: android android-intent icons

我在使用活动名称加载活动图标时遇到问题。

我有一个pakcageName和一个活动名称,实际上是类名

我想获得活动图标

    'private List<ResolveInfo> apps;
    private PackageManager pm;

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    pm = getPackageManager();
    apps = pm.queryIntentActivities(mainIntent, 0);
    ResolveInfo info = apps.get(position);

    pn =  info.activityInfo.packageName
    na = info.activityInfo.name

    imageView.setImageDrawable(pm.getActivityIcon(new ComponentName(pn, na)));'

2 个答案:

答案 0 :(得分:8)

这应该这样做:

public Drawable getActivityIcon(
        Context context, 
        String packageName, String activityName) {

    PackageManager packageManager = context.getPackageManager();

    Intent intent = new Intent();
    intent.setComponent(new ComponentName(packageName, activityName));
    ResolveInfo resolveInfo = packageManager.resolveActivity(intent, 0);

    return resolveInfo.loadIcon(packageManager);
}

答案 1 :(得分:-3)

您可以通过以下方式找到您的应用程序图标:

1 - 检查清单中的应用程序图标

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

2 - 得到它:

imageView.setImageDrawable(R.drawable.ic_launcher));