本文将介绍如何获取设备中已经安装的应用信息,包括:应用名称、包名、图标等。
获得信息列表后,选择某一项记录还可以启动对应的应用!
1.代码实现
package com.example.showapplist; import java.util.ArrayList; import java.util.List; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; public class AppListFragment extends ListFragment { private ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getAppList(); AppAdapter adapter = new AppAdapter(this.getActivity(), appList); setListAdapter(adapter); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onListItemClick(ListView l, View v, int position, long id) { // 启动所选应用 startActivity(appList.get(position).appIntent); } /** * 获取非系统应用信息列表 */ private void getAppList() { PackageManager pm = this.getActivity().getPackageManager(); // Return a List of all packages that are installed on the device. List<PackageInfo> packages = pm.getInstalledPackages(0); for (PackageInfo packageInfo : packages) { // 判断系统/非系统应用 if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) // 非系统应用 { AppInfo info = new AppInfo(); info.appName = packageInfo.applicationInfo.loadLabel(pm) .toString(); info.pkgName = packageInfo.packageName; info.appIcon = packageInfo.applicationInfo.loadIcon(pm); // 获取该应用安装包的Intent,用于启动该应用 info.appIntent = pm.getLaunchIntentForPackage(packageInfo.packageName); appList.add(info); } else { // 系统应用 } } } }
其中,getAppList()方法获得了当前设备所安装的应用信息。
通过以下代码判断某一应用是系统应用还是非系统应用:
// 判断系统/非系统应用 if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) // 非系统应用 { } else { // 系统应用 }
// 获取该应用安装包的Intent,用于启动该应用 info.appIntent = pm.getLaunchIntentForPackage(packageInfo.packageName);
2.工程源码
http://download.csdn.net/detail/chadeltu/8554415
原文地址:http://blog.csdn.net/chadeltu/article/details/44808887