本文介绍了Android的:如何获得安装活动的清单,因为它们出现在发射器,没有重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序,允许用户查看已安装的应用程序列表,选择其中一个,然后启动按计划进行。从使用计算器我管理教程,以弄清楚如何让安装活动的清单,他们的包名和图标(即here - 多种方式来做到这一点)。以防万一,我这是怎么开始的活动,它完美的作品,没有问题就在这里:

 意图launchIntent = packageManager.getLaunchIntentForPackage(的packageName);
launchIntent.setAction(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launchIntent);
 

问题是与检索已安装应用程序的列表。我发现两种方式获取安装的应用程序的列表:

1)使用

  PackageManager PM = getPackageManager();
名单< ApplicationInfo>应用程序= pm.getInstalledApplication(PackageManager.GET_META_DATA)
 

和从应用程序的每个元素就可以得到它的包名称和包装标签(应用程序名称)。

2)使用

  PackageManager PM = getPackageManager();
意图mainIntent =新的意图(Intent.ACTION_MAIN,NULL);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
名单< ResolveInfo> resolveInfos = packageManager.queryIntentActivities(mainIntent,0);
对于(ResolveInfo信息:resolveInfos){
    ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;
    // ...
    //从applicationInfo对象包名称,图标和标签
}
 

有一个与第一方式的一个问题:它返回所有已安装的软件包,包括系统服务,这可能不包含任何活动,因此没有可启动。下面是一个例子截图:

上面所有的项目都没有图标是不是可启动的。

有一个与第二方法的问题,以及:在列表中有重复的几个项目:

当我设置的调试器,我看到一个断点,这些地图的项目有不同的活动名称(com.google.android.maps.MapsActivity,com.google.android.maps.LatitudeActivity, com.google.android.maps.PlacesActivity等)。

我决定用第二种方法,因为它给一个列表,更适合我的需求,但我无法找到一个方法来过滤掉重复,只显示了应用程序的默认行为,因为他们出现在启动程序(你只看到一个人地图在应用程序手机的名单,不是四个)。我试图通过 ApplicationInfo.FLAG_SYSTEM 过滤掉系统的应用程序,但这种删除许多应用程序,我想有,包括地图和其他preinstalled应用程序。我执行queryIntentActivities在使用 PackageManager.MATCH_DEFAULT_ONLY 标志试过了,但是这也过滤掉许多应用程序,只留下几个。

我有点丢在这里了,我不知道该怎么办。我读过所有关于计算器的问题有关检索已安装应用程序的列表,但这个问题从来没有被提出了。请帮助任何人?如何取回,有没有重复安装了可启动的应用程序的列表?

解决方案

 意图localIntent2 =新的意向书(android.intent.action.PICK_ACTIVITY);
意图localIntent3 =新的意向书(android.intent.action.MAIN,NULL);
localIntent3.addCategory(android.intent.category.LAUNCHER);
localIntent2.putExtra(android.intent.extra.INTENT,localIntent3);
startActivityForResult(localIntent2,1);
 

试试这个code。它会列出只有它们都在设备中安装的应用程序。

I am writing an app that allows user to view the list of installed apps, select one of them and then start it on schedule. Using tutorials from stackoverflow i managed to figure out how to get a list of installed activities, their package names and icons(i.e. here - several ways to do it). Just in case, this is how i start activities, it works flawlessly, no problem here:

Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
launchIntent.setAction(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launchIntent);

The problem is with retrieving a list of installed apps. I've found two ways to get a list of installed applications:

1) use

PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplication(PackageManager.GET_META_DATA) 

and from each element from apps you can get it's package name and package label(app names).

2) use

PackageManager pm = getPackageManager();    
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(mainIntent, 0);
for(ResolveInfo info : resolveInfos) {
    ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;
    //...
    //get package name, icon and label from applicationInfo object    
}

There is a problem with first method: it returns all installed packages, including system services, which may not contain any activity and are therefore not launchable. Here's a screenshot with an example:

All the items above that have no icons are not launchable.

There is a problem with the second approach as well: Several items in the list have duplicates:

When i set up a breakpoint in debugger i see, that these "Maps" items have different activity names ("com.google.android.maps.MapsActivity", "com.google.android.maps.LatitudeActivity", "com.google.android.maps.PlacesActivity" etc.).

I decided to use the second approach, because it gives a list that is more suitable for my needs, but i can't find a way to filter out the duplicates and only show the default activity for the app, as they appear in the Launcher(you only see one 'Maps' in your phone's list of apps, not four). I've tried filtering out system apps through ApplicationInfo.FLAG_SYSTEM, but this removes many apps that i want to have, including Maps and other preinstalled apps. I've tried using PackageManager.MATCH_DEFAULT_ONLY flag when executing queryIntentActivities, but this also filters out many apps, leaving just a few.

I'm kinda lost here, and i don't know what to do. I've read all the questions on stackoverflow about retrieving a list of installed apps, but this issue has never been brought up. Please, help anyone? How do i retrieve a list of installed launchable apps that has no duplicates?

解决方案
Intent localIntent2 = new Intent("android.intent.action.PICK_ACTIVITY");
Intent localIntent3 = new Intent("android.intent.action.MAIN",null);
localIntent3.addCategory("android.intent.category.LAUNCHER");   
localIntent2.putExtra("android.intent.extra.INTENT",localIntent3);
startActivityForResult(localIntent2, 1);

Try this code. It will list out only the applications which are all installed in your device.

这篇关于Android的:如何获得安装活动的清单,因为它们出现在发射器,没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:42