简体   繁体   English

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

[英]Get icons of all installed apps in android

I want to get icons of my all installed apps.我想获取我所有已安装应用程序的图标。 Can I get that icons using package manager?我可以使用包管理器获取那个图标吗? Is there any function for it?它有什么功能吗? Or any other way to get icons of all installed apps in bitmap?或者任何其他方式来获取位图中所有已安装应用程序的图标?

Thanks!谢谢!

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

}

Check here for more details.查看此处了解更多详情。

Above answers are pretty good.楼上的回答都不错。

Your Question is:- Get icons of all installed apps in android?您的问题是:-获取 android 中所有已安装应用的图标? you want list of install apps icon你想要安装应用程序图标的列表

Here is the code which help you to get install apps list with Application (icons,packages names) .这是帮助您使用 Application (icons,packages names)获取安装应用程序列表的代码。

**Declare variable in your Activity**

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

Just call below function loadApps() in your Activity onCreate()只需在您的活动 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();
    }
}

Here is Link for reference这是链接供参考

OR要么

You can download custom launcher code from My Github repository您可以从 My Github存储库下载自定义启动器代码

Try this way: Make a class called PackageInformation :试试这个方法:创建一个名为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;
    }

}

tuck this away somewhere and now to access the info from your working Activity class do this:把它藏在某个地方,现在从你的工作 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;

}

I find it the easiest way:我觉得最简单的方法:

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);
}

Now to get the icons, use this:现在要获取图标,请使用:

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

Here below is the code with which you can get the icons of all installed Apps.以下是您可以获取所有已安装应用程序图标的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Android 中获取并列出所有已安装/可用的导航应用程序及其图标? - How to get and list all the installed/available navigations apps along with their icons in Android? 如何在Android手机上安装所有应用程序 - How to get all apps installed on android phone Android获取所有已安装应用的列表 - Android get a list of all installed apps 获取android中所有已安装应用的缓存目录 - Get Cache Directory for all installed apps in android 获取android 11(API 30)中所有已安装的应用程序,如何获取android 11中安装的所有其他应用程序? - Get all installed apps in android 11 (API 30) , how to get all other apps installed in android 11? 在Android中获取所有已安装的应用程序图标:java.lang.ClassCastException - Get All Installed Application icons in Android : java.lang.ClassCastException Android会在某些首次安装日期安装所有应用程序 - Android get all Apps installed on certain First Install Dates 以编程方式存储和检索所有已安装应用程序的应用程序图标 - Storing and retrieving app icons of all installed apps programmatically Android应用中的所有图片都被视为图标吗? - Are all images in Android apps considered Icons? 从 Android 上安装的应用程序中获取非自适应图标 - Getting non-adaptive icons from installed apps on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM