本周早些时候我问了一个类似的问题,但我仍然不了解如何获取所有已安装应用程序的列表,然后选择一个运行.
我试过了:
Intent intent = new Intent(ACTION_MAIN); intent.addCategory(CATEGORY_LAUNCHER);
这只显示预先安装的应用程序或可以运行ACTION_MAIN Intent类型的应用程序.
我也知道我可以使用PackageManager来获取所有已安装的应用程序,但是如何使用它来运行特定的应用程序呢?
这是使用packageManager的一种更简洁的方法
final PackageManager pm = getPackageManager(); //get a list of installed apps. Listpackages = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo packageInfo : packages) { Log.d(TAG, "Installed package :" + packageInfo.packageName); Log.d(TAG, "Source dir : " + packageInfo.sourceDir); Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); } // the getLaunchIntentForPackage returns an intent that you can use with startActivity()
更多信息,请访问http://qtcstation.com/2011/02/how-to-launch-another-app-from-your-app/
以下是获取Android上安装的活动/应用程序列表的代码:
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); ListpkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
您将获得ResolveInfo
启动应用程序中的所有必要数据.你可以在这里查看ResolveInfo
javadoc .
过滤系统应用程序的另一种方法(使用上面的示例):
/** * Return whether the given PackageInfo represents a system package or not. * User-installed packages (Market or otherwise) should not be denoted as * system packages. * * @param pkgInfo * @return */ private boolean isSystemPackage(PackageInfo pkgInfo) { return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0); }
这是一个很好的例子:
class PInfo { private String appname = ""; private String pname = ""; private String versionName = ""; private int versionCode = 0; private Drawable icon; private void prettyPrint() { Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode); } } private ArrayListgetPackages() { ArrayList apps = getInstalledApps(false); /* false = no system packages */ final int max = apps.size(); for (int i=0; i getInstalledApps(boolean getSysPackages) { ArrayList res = new ArrayList (); List packs = getPackageManager().getInstalledPackages(0); for(int i=0;i
5> XXX..:获取已安装的非系统应用程序列表
public static void installedApps() { ListpackList = getPackageManager().getInstalledPackages(0); for (int i=0; i < packList.size(); i++) { PackageInfo packInfo = packList.get(i); if ( (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString(); Log.e("App ? " + Integer.toString(i), appName); } } }
6> 小智..:要过滤基于系统的应用:
private boolean isSystemPackage(ResolveInfo ri) { return (ri.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; }
7> 小智..:要获得已安装的应用程序,您可以使用包管理器..
Listapps = getPackageManager().getInstalledPackages(0); 要运行,您可以使用包名称
Intent launchApp = getPackageManager().getLaunchIntentForPackage(“package name”) startActivity(launchApp);有关更多详细信息,请阅读此博客http://codebucket.co.in/android-get-list-of-all-installed-apps/
8> 小智..:您可以使用以下代码在Android设备中查找已安装应用程序的列表,"packageInfo"包含设备中已安装的应用程序信息.我们可以为从packageinfo对象安装的应用程序检索Intent并使用startactivity(intent),可以启动应用程序.您可以通过Listview或Gridview来组织UI.因此,基于位置的点击事件,您可以检索意图对象并启动活动意图.
final PackageManager pm = getPackageManager(); Listpackages = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo packageInfo : packages) { if(pm.getLaunchIntentForPackage(packageInfo.packageName)!= null && !pm.getLaunchIntentForPackage(packageInfo.packageName).equals("")) { System.out.println("Package Name :" + packageInfo.packageName); System.out.println("Launch Intent For Package :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); System.out.println("Application Label :" + pm.getApplicationLabel(packageInfo)); System.out.println("Application Label :" + pm.getApplicationIcon(packageInfo.packageName).toString()); System.out.println("i : "+i); /*if(i==2) { startActivity(pm.getLaunchIntentForPackage(packageInfo.packageName)); break; }*/ i++; } }
9> joecizac..:我要求过滤掉用户并不真正使用的系统应用程序(例如"com.qualcomm.service","update services"等).最后,我添加了另一个条件来过滤应用列表.我刚刚检查了该应用是否具有'启动器意图'.
所以,结果代码看起来像......
PackageManager pm = getPackageManager(); Listapps = pm.getInstalledApplications(PackageManager.GET_GIDS); for (ApplicationInfo app : apps) { if(pm.getLaunchIntentForPackage(app.packageName) != null) { // apps with launcher intent if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) { // updated system apps } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { // system apps } else { // user installed apps } appsList.add(app); } }
10> kakopappa..:如果一个包中有多个启动器,则上面的代码有问题.例如:在用于LG的LG Optimus Facebook,用于LG的MySpace,用于LG的Twitter包含一个包名称SNS,如果您使用上面的SNS将重复.经过几个小时的研究,我得到了以下代码.似乎运作良好.
private ListgetInstalledComponentList() throws NameNotFoundException { final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); List ril = getPackageManager().queryIntentActivities(mainIntent, 0); List componentList = new ArrayList (); String name = null; for (ResolveInfo ri : ril) { if (ri.activityInfo != null) { Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo); if (ri.activityInfo.labelRes != 0) { name = res.getString(ri.activityInfo.labelRes); } else { name = ri.activityInfo.applicationInfo.loadLabel( getPackageManager()).toString(); } componentList.add(name); } } return componentList; }
11> Spidey..:@Jas:我已经没有那个代码了,但我发现了一些接近的东西.我已经这样做来搜索我的应用程序的"组件",它们只是具有给定类别的活动.
private ListgetInstalledComponentList() { Intent componentSearchIntent = new Intent(); componentSearchIntent.addCategory(Constants.COMPONENTS_INTENT_CATEGORY); componentSearchIntent.setAction(Constants.COMPONENTS_INTENT_ACTION_DEFAULT); List ril = getPackageManager().queryIntentActivities(componentSearchIntent, PackageManager.MATCH_DEFAULT_ONLY); List componentList = new ArrayList (); Log.d(LOG_TAG, "Search for installed components found " + ril.size() + " matches."); for (ResolveInfo ri : ril) { if (ri.activityInfo != null) { componentList.add(ri.activityInfo.packageName);// + ri.activityInfo.name); Log.d(LOG_TAG, "Found installed: " + componentList.get(componentList.size()-1)); } } return componentList; } 我已经评论了它获取活动名称的部分,但它非常简单.
12> funcoder..:清洁解决方案,成功过滤系统应用程序
这个解决方案背后的想法是每个系统应用程序的主要活动没有自定义活动图标.这种方法给了我一个很好的结果:
public static SetgetInstalledApps(Context ctx) { final PackageManager packageManager = ctx.getPackageManager(); final List allInstalledPackages = packageManager.getInstalledPackages(PackageManager.GET_META_DATA); final Set filteredPackages = new HashSet(); Drawable defaultActivityIcon = packageManager.getDefaultActivityIcon(); for(PackageInfo each : allInstalledPackages) { if(ctx.getPackageName().equals(each.packageName)) { continue; // skip own app } try { // add only apps with application icon Intent intentOfStartActivity = packageManager.getLaunchIntentForPackage(each.packageName); if(intentOfStartActivity == null) continue; Drawable applicationIcon = packageManager.getActivityIcon(intentOfStartActivity); if(applicationIcon != null && !defaultActivityIcon.equals(applicationIcon)) { filteredPackages.add(each); } } catch (PackageManager.NameNotFoundException e) { Log.i("MyTag", "Unknown package name " + each.packageName); } } return filteredPackages; }