インストールしているアプリの情報を取得する
こんなコードが基本。
PackageManager pm = context.getPackageManager(); ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0); String appFile = appInfo.sourceDir; long installed = File(appFile).lastModified();
以前、こういうのはやってみたが、なんかメンドクサイ。
単純に「インストールしているアプリケーションの一覧」が欲しいだけなのだが。
アプリケーションリストを取得する から。
// パッケージマネージャを利用 PackageManager pm = getPackageManager(); // インテントで取得するアクティビティ種類を指定 Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); // 該当するアクティビティの情報たちを取得 List<ResolveInfo> appsInfo = pm.queryIntentActivities(intent, 0); // それぞれのアクティビティ内の取得する情報を指定 for (ResolveInfo info : appsInfo) { ActivityInfo activityInfo = info.activityInfo; CharSequence labelSeq = info.loadLabel(pm); String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name; // アプリ内の取得する情報を取得 sb.append(label + "\n"); }
Intentとか使わずに。
PackageManager pm = context.getPackageManager(); List<ApplicationInfo> appInfoList = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo info : appInfoList) { sb.append(info.processName + "\n"); }
結局、以下の2行だと。
PackageManager pm = getPackageManager(); List<ApplicationInfo> applicationInfoList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
Constantsに関しては、
http://developer.android.com/intl/ja/reference/android/content/pm/PackageManager.html#constants
アプリケーション名(日本語とか)が欲しいのだが、
サンプルアプリのホームでは、以下のような記述。
/** * Loads the list of installed applications in mApplications. */ private void loadApplications(boolean isLaunching) { if (isLaunching && mApplications != null) { return; } PackageManager manager = getPackageManager(); Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0); Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager)); if (apps != null) { final int count = apps.size(); if (mApplications == null) { mApplications = new ArrayList<ApplicationInfo>(count); } mApplications.clear(); for (int i = 0; i < count; i++) { ApplicationInfo application = new ApplicationInfo(); ResolveInfo info = apps.get(i); application.title = info.loadLabel(manager); application.setActivity(new ComponentName( info.activityInfo.applicationInfo.packageName, info.activityInfo.name), Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); application.icon = info.activityInfo.loadIcon(manager); mApplications.add(application); } } }
結局こうか。
private void setListApps() { PackageManager pm = getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0); Collections.sort(infos, new ResolveInfo.DisplayNameComparator(pm)); for (ResolveInfo info : infos) { apps.add(info.loadLabel(pm).toString()); } }