如何获取已安装的 android 应用程序列表并选择一个运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2695746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a list of installed android applications and pick one to run
提问by 2Real
I asked a similar question to this earlier this week but I'm still not understanding how to get a list of all installed applications and then pick one to run.
本周早些时候我问了一个类似的问题,但我仍然不明白如何获取所有已安装应用程序的列表,然后选择一个来运行。
I've tried:
我试过了:
Intent intent = new Intent(ACTION_MAIN);
intent.addCategory(CATEGORY_LAUNCHER);
and this only shows application that are preinstalled or can run the ACTION_MAIN
Intent type.
这仅显示预安装或可以运行ACTION_MAIN
Intent 类型的应用程序。
I also know I can use PackageManager
to get all the installed applications, but how do I use this to run a specific application?
我也知道我可以使用它PackageManager
来获取所有已安装的应用程序,但是如何使用它来运行特定的应用程序?
采纳答案by Karan
Following is the code to get the list of activities/applications installed on Android :
以下是获取 Android 上安装的活动/应用程序列表的代码:
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
You will get all the necessary data in the ResolveInfo
to start a application. You can check ResolveInfo
javadoc here.
您将获得ResolveInfo
启动应用程序所需的所有数据。您可以在此处查看ResolveInfo
javadoc 。
回答by Nelson Ramirez
Here's a cleaner way using the PackageManager
这是使用 PackageManager
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = 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()
More info here http://qtcstation.com/2011/02/how-to-launch-another-app-from-your-app/
更多信息在这里http://qtcstation.com/2011/02/how-to-launch-another-app-from-your-app/
回答by Kenneth Evans
Another way to filter on system apps (works with the example of king9981):
另一种过滤系统应用程序的方法(以king9981为例):
/**
* 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);
}
回答by king9981
Here a good example:
这是一个很好的例子:
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 ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
final int max = apps.size();
for (int i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
return apps;
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
回答by XXX
Getting list of installed non-system apps
获取已安装的非系统应用程序列表
public static void installedApps()
{
List<PackageInfo> packList = 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);
}
}
}
回答by SamCsharpAs3
To filter on sytem based apps :
要过滤基于系统的应用程序:
private boolean isSystemPackage(ResolveInfo ri) {
return (ri.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
}
回答by Arvind
To get al installed apps you can use Package Manager..
要获得所有已安装的应用程序,您可以使用包管理器..
List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
To run you can use package name
要运行,您可以使用包名
Intent launchApp = getPackageManager().getLaunchIntentForPackage(“package name”)
startActivity(launchApp);
For more detail you can read this blog http://codebucket.co.in/android-get-list-of-all-installed-apps/
有关更多详细信息,您可以阅读此博客http://codebucket.co.in/android-get-list-of-all-installed-apps/
回答by Prashant Agrawal
You can Find the List of installed apps in Android Device by using below code, "packageInfo" Contains Installed Application Information in Device. we can retrive Intent for the application installed from the packageinfo object and by using startactivity(intent), can start application. it is up to you how you organize the UI either Listview or Gridview. so on click event based on position, you can retrive intent object and start activity intent.
您可以使用以下代码在 Android 设备中查找已安装的应用程序列表,“packageInfo”包含设备中已安装的应用程序信息。我们可以从 packageinfo 对象检索安装的应用程序的意图,并通过使用 startactivity(intent),可以启动应用程序。由您决定如何组织 UI,无论是 Listview 还是 Gridview。所以基于位置的点击事件,您可以检索意图对象并启动活动意图。
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = 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++;
}
}
回答by joecizac
I had a requirement to filter out the system apps which user do not really use(eg. "com.qualcomm.service", "update services", etc). Ultimately I added another condition to filter down the app list. I just checked whether the app has 'launcher intent'.
我需要过滤掉用户并不真正使用的系统应用程序(例如“com.qualcomm.service”、“更新服务”等)。最终我添加了另一个条件来过滤应用程序列表。我刚刚检查了该应用程序是否具有“启动器意图”。
So, the resultant code looks like...
所以,结果代码看起来像......
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = 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);
}
}
回答by kakopappa
If there are multiple launchers in a one package above code has a problem. Eg: on LG Optimus Facebook for LG, MySpace for LG, Twitter for LG contains in a one package name SNS and if you use above SNS will repeat. After hours of research I came with below code. Seems to work well.
如果一个包中有多个启动器,上面的代码就有问题。例如:在 LG Optimus Facebook for LG、MySpace for LG、Twitter for LG 上包含一个包名称 SNS,如果您使用上面的 SNS 将重复。经过数小时的研究,我得到了以下代码。似乎运作良好。
private List<String> getInstalledComponentList()
throws NameNotFoundException {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> ril = getPackageManager().queryIntentActivities(mainIntent, 0);
List<String> componentList = new ArrayList<String>();
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;
}