簡體   English   中英

獲取android中所有已安裝應用的圖標

[英]Get icons of all installed apps in android

我想獲取我所有已安裝應用程序的圖標。 我可以使用包管理器獲取那個圖標嗎? 它有什么功能嗎? 或者任何其他方式來獲取位圖中所有已安裝應用程序的圖標?

謝謝!

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

}

查看此處了解更多詳情。

樓上的回答都不錯。

您的問題是:-獲取 android 中所有已安裝應用的圖標? 你想要安裝應用程序圖標的列表

這是幫助您使用 Application (icons,packages names)獲取安裝應用程序列表的代碼。

**Declare variable in your Activity**

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

只需在您的活動 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();
    }
}

這是鏈接供參考

要么

您可以從 My Github存儲庫下載自定義啟動器代碼

試試這個方法:創建一個名為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;

}

我覺得最簡單的方法:

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

以下是您可以獲取所有已安裝應用程序圖標的代碼。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM