本周早些时候我提出了一个类似的问题,但我仍然不明白如何获取所有已安装应用程序的列表,然后选择其中一个运行。
我尝试过:
Intent intent = new Intent(ACTION_MAIN);
intent.addCategory(CATEGORY_LAUNCHER);
这只显示预安装或可以运行 ACTION_MAIN
Intent 类型的应用程序。
我还知道我可以使用 PackageManager
获取所有已安装的应用程序,但是如何使用它来运行特定的应用程序呢?
本周早些时候我提出了一个类似的问题,但我仍然不明白如何获取所有已安装应用程序的列表,然后选择其中一个运行。
我尝试过:
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.
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()
更多信息请访问 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);
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
您可以在ResolveInfo
中获取所有必要的数据来启动应用程序。您可以在此处查看 ResolveInfo
javadoc。
过滤系统应用的另一种方法(适用于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);
}
这里有一个很好的例子:
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;
}
获取已安装的非系统应用列表
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);
}
}
}
要过滤基于系统的应用程序:
private boolean isSystemPackage(ResolveInfo ri) {
return (ri.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
}
您可以使用“PackageManager”获取所有已安装的应用程序。
List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
要运行一个应用程序,您可以使用其软件包名称
Intent launchApp = getPackageManager().getLaunchIntentForPackage(“package name”)
startActivity(launchApp);
如果您需要更详细的信息,可以阅读这篇博客http://codebucket.co.in/android-get-list-of-all-installed-apps/
context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
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++;
}
}
我有一个要求,需要过滤掉用户实际上不使用的系统应用程序(例如"com.qualcomm.service", "update services"等)。最终,我添加了另一个条件来筛选应用列表。我只检查该应用是否具有'launcher intent'。
因此,最终的代码看起来像...
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);
}
}