Answers:
以下是获取安装在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 。
这是使用 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/
在系统应用程序上进行过滤的另一种方法(以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;
}
要获取所有已安装的应用程序,可以使用程序包管理器。
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/
您可以使用以下代码查找“ Android设备中已安装应用程序的列表”,“ packageInfo”包含设备中已安装应用程序的信息。我们可以检索从packageinfo对象安装的应用程序的Intent,并使用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++;
}
}
我有一个要求,即过滤掉用户未真正使用的系统应用程序(例如“ com.qualcomm.service”,“ update services”等)。最终,我添加了另一个条件来过滤应用程序列表。我只是检查了该应用程序是否具有“启动器意图”。
因此,结果代码看起来像...
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);
}
}
如果一个程序包中有多个启动器,则上述代码有问题。例如:在LG Optimus上,Facebook的LG,MySpace的LG,Twitter的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;
}
@Jas:我没有该代码了,但是我发现了一些接近的东西。我这样做是为了搜索应用程序的“组件”,它们只是具有给定类别的活动。
private List<String> getInstalledComponentList() {
Intent componentSearchIntent = new Intent();
componentSearchIntent.addCategory(Constants.COMPONENTS_INTENT_CATEGORY);
componentSearchIntent.setAction(Constants.COMPONENTS_INTENT_ACTION_DEFAULT);
List<ResolveInfo> ril = getPackageManager().queryIntentActivities(componentSearchIntent, PackageManager.MATCH_DEFAULT_ONLY);
List<String> componentList = new ArrayList<String>();
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;
}
我已经评论了获得活动名称的部分,但这很简单。
该解决方案背后的想法是,每个系统应用程序的主要活动都没有自定义活动图标。这种方法给我一个很好的结果:
public static Set<PackageInfo> getInstalledApps(Context ctx) {
final PackageManager packageManager = ctx.getPackageManager();
final List<PackageInfo> allInstalledPackages = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);
final Set<PackageInfo> 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;
}
private static boolean isThisASystemPackage(Context context, PackageInfo packageInfo ) {
try {
PackageInfo sys = context.getPackageManager().getPackageInfo("android", PackageManager.GET_SIGNATURES);
return (packageInfo != null && packageInfo.signatures != null &&
sys.signatures[0].equals(packageInfo.signatures[0]));
} catch (NameNotFoundException e) {
return false;
}
}
我有另一个解决方案:
ArrayList<AppInfo> myAppsToUpdate;
// How to get the system and the user apps.
public ArrayList<AppInfo> getAppsToUpdate() {
PackageManager pm = App.getContext().getPackageManager();
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
myAppsToUpdate = new ArrayList<AppInfo>();
for (ApplicationInfo aInfo : installedApps) {
if ((aInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// System apps
} else {
// Users apps
AppInfo appInfo = new AppInfo();
appInfo.setAppName(aInfo.loadLabel(pm).toString());
appInfo.setPackageName(aInfo.packageName);
appInfo.setLaunchActivity(pm.getLaunchIntentForPackage(aInfo.packageName).toString());
try {
PackageInfo info = pm.getPackageInfo(aInfo.packageName, 0);
appInfo.setVersionName(info.versionName.toString());
appInfo.setVersionCode("" + info.versionCode);
myAppsToUpdate.add(appInfo);
} catch (NameNotFoundException e) {
Log.e("ERROR", "we could not get the user's apps");
}
}
}
return myAppsToUpdate;
}
获取所有应用程序:
PackageManager pm = getContext().getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
检查是否已安装应用程序,然后打开:
if((app.flags & (ApplicationInfo.FLAG_UPDATED_SYSTEM_APP | ApplicationInfo.FLAG_SYSTEM)) > 0) {
String app_package = app.packageName;
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(app_package);
context.startActivity(launchIntent);
您可以使用此:
PackageManager pm = getApplicationContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
// facebook
}
if ((app.activityInfo.name).contains("android.gm"))
{
// gmail
}
if ((app.activityInfo.name).contains("mms"))
{
// android messaging app
}
if ((app.activityInfo.name).contains("com.android.bluetooth"))
{
// android bluetooth
}
}