目前面世的许多软件中有这么一个功能:设备中安装了哪些软件,他们会以一个软件列表清单的形式向用户展示出来。
今天我们就来实现这一功能:
运行环境: motorola defy+ 系统2.3.6(本吊的吊丝机)
主要 API : PackageInfo,PackageManager,LayoutInflater,ApplicationInfo
PackageManger类,它的主要职责是管理应用程序包。 通过它,我们就可以获取应用程序信息
通过PackageManager获取应用程序相关信息,再通过listView显示出相应信息。
直接上主要代码了
public class AppListView extends LinearLayout implements OnItemClickListener{ private final static String TAG = "AppListView"; private ListView mListView; private TextView mTvTitle; private List<AppInfo> mAppList; private Context mContext; private LayoutInflater mInflater; private PackageManager mPacManager; public AppListView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public AppListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public AppListView(Context context) { super(context); init(context); } private void init(Context c) { mContext = c; mInflater = (LayoutInflater)c.getSystemService (Context.LAYOUT_INFLATER_SERVICE); this.addView(mInflater.inflate(R.layout.activity_main,null,false)); mListView = (ListView)this.findViewById(R.id.listView); mTvTitle = (TextView)this.findViewById(R.id.title); loadAppData(); mListView.setAdapter(new MyAdapter(c)); mListView.setOnItemClickListener(this); Log.d(TAG, "一共"+mAppList.size()); } // 加载应用软件数据 软件名称,包名,对应的图标等等 private void loadAppData() { if(mAppList != null){ mAppList.clear(); }else{ mAppList = new ArrayList<AppInfo>(); } mPacManager = mContext.getPackageManager(); List<PackageInfo> packages = mPacManager.getInstalledPackages(0); for(int i=0; i<packages.size(); i++){ PackageInfo pi = packages.get(i); AppInfo ai = new AppInfo(); ai.packageName = pi.packageName; ai.appName = pi.applicationInfo.loadLabel(mPacManager).toString(); ai.appIcon = pi.applicationInfo.loadIcon(mPacManager); mAppList.add(ai); } mTvTitle.setText("本机所安装的软件总数:"+packages.size()+"个"); } // 为ListView自定义适配器 class MyAdapter extends BaseAdapter { public MyAdapter(Context c) { mContext = c; } @Override public int getCount() { return mAppList == null?0:mAppList.size(); } @Override public Object getItem(int arg0) { return mAppList == null?null:mAppList.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { View view; if(arg1 == null){ view = mInflater.inflate(R.layout.app_list_item, null); }else{ view = arg1; } AppInfo ai = (AppInfo)getItem(arg0); ImageView appIcon = (ImageView)view.findViewById(R.id.appIcon); TextView appName = (TextView)view.findViewById(R.id.appName); TextView appPackage = (TextView)view.findViewById(R.id.appPackage); appIcon.setImageDrawable(ai.appIcon); appName.setText(ai.appName); appPackage.setText(ai.packageName); return view; } } // 处理ListView的item的点击操作,我这里是启动该应用程序 @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(mContext, arg2+"", Toast.LENGTH_SHORT).show(); // 获取本次item的包名 String packName = mAppList.get(arg2).packageName; // 启动此程序 Intent intent = mPacManager.getLaunchIntentForPackage(packName); mContext.startActivity(intent); } // 软件载体 public class AppInfo { // 软件名称 public String appName=""; // 软件包名 public String packageName=""; // 软件图标 public Drawable appIcon=null; } }
public abstract Intent getLaunchIntentForPackage (String packageName)
这个方法通过包名返回一个Intent , 然后通过StartActivity(Intent)启动应用程序
源码
下面是程序运行的效果图
扩展阅读:
android下判断一个应用app是否是系统rom集成的方法