web-dev-qa-db-ja.com

インストールされているすべてのアプリのアイコンをandroid

インストールされているすべてのアプリのアイコンを取得したい。パッケージマネージャーを使用してそのアイコンを取得できますか?そのための機能はありますか?または、インストールされているすべてのアプリのアイコンをビットマップで取得する他の方法はありますか?

ありがとう!

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

}

詳細については、 ここ を確認してください。

43
Ponmalar

上記の答えはかなり良いです。

あなたの質問は:-Androidにインストールされているすべてのアプリのアイコンを取得しますか?インストールアプリのアイコンのリストが必要です

これは、アプリケーション(アイコン、パッケージ名)でインストールアプリリストを取得するのに役立つコードです。

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

ここに リンク 参考のために

OR

カスタムランチャーコードはMy Github リポジトリからダウンロードできます

6
Arbaz.in

私はそれが最も簡単な方法だと思います:

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

この方法を試してください: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;
    }

}

これをどこかに隠して、作業中のアクティビティクラスからの情報にアクセスするには、次のようにします。

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;

}
4
j2emanue

以下は、インストールされているすべてのアプリのアイコンを取得するためのコードです。

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