我有listview
显示所有已安装的应用,包括一些系统应用,但不显示图库,联系人,消息应用。请告诉我如何获得所有这些系统应用程序。这是我的代码
public static List getInstalledApplication(Context c)
{
// return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
PackageManager pm = c.getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
for(ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//Discard this one
//in this case, it should be a user-installed app
// installedApps.add(app);
} else {
installedApps.add(app);
}
}
return installedApps;
}
告诉我哪里弄错了。帮我一些代码。
答案 0 :(得分:1)
您要列出的应用程序(图库,消息等)是在系统分区中编写的,因此(app.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
将为真。
FLAG_SYSTEM
if set, this application is installed in the device's system image.
这就是为什么你想要列出的应用程序也会被跳过。
如果您想获得启动器中列出的应用,即类别为<Launcher>
的应用,请使用以下代码获取列表
final PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent, 0);
// using hashset so that there will be no duplicate packages,
// if no duplicate packages then there will be no duplicate apps
HashSet<ApplicationInfo> installedApps = new HashSet<ApplicationInfo>(0);
// getting package names and adding them to the hashset
for (ResolveInfo resolveInfo : resInfos) {
installedApps.add(resolveInfo.activityInfo.applicationInfo);
}
答案 1 :(得分:1)
试试此代码
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()
编辑1
PackageManager pm = getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
for (ResolveInfo resolveInfo : lst) {
Log.d("Test", "New Launcher Found: " + resolveInfo.activityInfo.packageName);
}
这可能有助于你
答案 2 :(得分:0)
因为您要显示所有已安装的应用。您可以删除代码中的if-else块,只需添加所有应用程序并显示它们即可。 或者在你的代码中进行这些更改
1)通过此代码获取所有应用。
List<ApplicationInfo> apps = getPackageManager().getInstalledPackages(0);
2)使用以下代码安装用户的单独系统应用程序:
List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0);
for(ApplicationInfo app : apps) {
if((app.flags & (ApplicationInfo.FLAG_UPDATED_SYSTEM_APP | ApplicationInfo.FLAG_SYSTEM)) > 0) {
// It is a system app
} else {
// It is installed by the user
}
}
答案 3 :(得分:0)
尝试这种不同的方式。
//首先
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = context.getPackageManager().queryIntentActivities(mainIntent, 0);
//第二次
PackageManager pm = getPackageManager();
List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages)
{
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
//第三次
private List getInstalledComponentList()
throws NameNotFoundException {
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;
}