在android中获取所有已安装应用的图标

时间:2012-05-22 05:23:16

标签: android android-package-managers

我想获取所有已安装应用的图标。我可以使用包管理器获取该图标吗?它有什么功能吗?或者以任何其他方式获取位图中所有已安装应用的图标?

谢谢!

5 个答案:

答案 0 :(得分:37)

try {
    String pkg = "com.app.my";//your package name
    Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);
    imageView.setImageDrawable(icon);
} catch (PackageManager.NameNotFoundException ne) {

}

查看here了解详情。

答案 1 :(得分:4)

尝试这种方式:创建一个名为PackageInformation的类:

public class PackageInformation {

    private Context mContext;

    public PackageInformation(Context context) {
        mContext = context;
    }

    class InfoObject {
        public String appname = "";
        public String pname = "";
        public String versionName = "";
        public int versionCode = 0;
        public Drawable icon;


        public void InfoObjectAggregatePrint() { //not used yet
            Log.v(appname, appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
        }

    }

    private ArrayList < InfoObject > getPackages() {
        ArrayList < InfoObject > apps = getInstalledApps(false);
        final int max = apps.size();
        for (int i = 0; i < max; i++) {
            apps.get(i).prettyPrint();
        }
        return apps;
    }

    public ArrayList < InfoObject > getInstalledApps(boolean getSysPackages) {
        ArrayList < InfoObject > res = new ArrayList < InfoObject > ();
        List < PackageInfo > packs = mContext.getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < packs.size(); i++) {
            PackageInfo p = packs.get(i);
            if ((!getSysPackages) && (p.versionName == null)) {
                continue;
            }
            InfoObject newInfo = new InfoObject();
            newInfo.appname = p.applicationInfo.loadLabel(mContext.getPackageManager()).toString();
            newInfo.pname = p.packageName;
            newInfo.versionName = p.versionName;
            newInfo.versionCode = p.versionCode;
            newInfo.icon = p.applicationInfo.loadIcon(mContext.getPackageManager());
            res.add(newInfo);
        }
        return res;
    }

}

把它扔到某个地方然后现在从你工作的Activity类中访问信息:

PackageInformation androidPackagesInfo = new PackageInformation(this);
ArrayList < InfoObject > appsData = androidPackagesInfo.getInstalledApps(true);

for (InfoObject info: appsData) {
    Toast.makeText(MainActivity.this, info.appname, 2).show();
    Drawable somedrawable = info.icon;

}

答案 2 :(得分:4)

我认为这是最简单的方法:

private List<ResolveInfo> installedApps() {
    final Intent main_intent = new Intent(Intent.ACTION_MAIN, null);
    main_intent.addCategory(Intent.CATEGORY_LAUNCHER);
    return package_manager.queryIntentActivities(main_intent, 0);
}

现在要获取图标,请使用:

for(ResolveInfo ri : installedApps()) { 
    // to get drawable icon -->  ri.loadIcon(package_manager)
}

答案 3 :(得分:3)

我知道我对这个答案来说太迟了。

以上答案非常好。

您的问题是: - 获取Android中所有已安装应用的图标? 你想要安装应用程序图标列表

以下代码可帮助您使用应用(图标,包名称)获取安装应用列表。

**Declare variable in your Activity**

private CustomAppListAdapter customAppListAdapter;
private ArrayList<AppListMain> appListMainArrayList;
private AppListMain appListMain;

只需在Activity onCreate()

中调用以下函数loadApps()
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_list);
    loadApps();

}

public void loadApps() {
    try {
        packageManager = getPackageManager();
        appListMainArrayList = new ArrayList<>();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);

        for (ResolveInfo resolveInfo : resolveInfoList) {
            AppListMain appListMain = new AppListMain();
            appListMain.setAppIcon(resolveInfo.activityInfo.loadIcon(packageManager));
            appListMain.setAppName(resolveInfo.loadLabel(packageManager).toString());
            appListMain.setAppPackage(resolveInfo.activityInfo.packageName);
            appListMainArrayList.add(appListMain);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

以下是Link以供参考

OR

您可以从我的Github存储库下载自定义启动器代码

答案 4 :(得分:1)

以下是用于获取所有已安装应用程序图标的代码。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                // try getting the properly colored launcher icons
                LauncherApps launcher = (LauncherApps) this.getSystemService(LAUNCHER_APPS_SERVICE);
                List<LauncherActivityInfo> activityList = launcher.getActivityList(packageName, android.os.Process.myUserHandle());
                drawable = activityList.get(0).getBadgedIcon(0);
            } catch (Exception e) {
            }
        }

        if (drawable == null) {

            try {
                getPackageManager().getApplicationIcon(packageName);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }