二、DEMO讲解
通过前面的介绍,相信您一定很了解了,本质上来讲,这些XXXInfo类不过是我们在AndroidManifest.XML文件中定义的信息,
知道到这点了,理解起来就不是很难了。
下面我透过两个简答的DEMO,来学以致用。
Demo 1: 通过queryIntentActivities()方法,查询Android系统的所有具备ACTION_MAIN和CATEGORY_LAUNCHER
的Intent的应用程序,点击后,能启动该应用,说白了就是做一个类似Home程序的简易Launcher 。
Demo 2 :通过getInstalledApplications()方法获取应用,然后对其过滤,查找出我们需要的第三方应用,系统应用,安装在sdcard的应用。
Demo1 :
图:

1 、布局文件: 主要有两个:带listview的browse_app_list.xml文件 ;listview的项browse_app_item.xml
browse_app_list.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">>
- <ListView android:id="@+id/listviewApp" android:layout_width="fill_parent"
- android:layout_height="fill_parent" ></ListView>
- </LinearLayout>
browse_app_item.xmlbrowse_app_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent" android:layout_height="50dip">
- <ImageView android:id="@+id/imgApp" android:layout_width="wrap_content"
- android:layout_height="fill_parent" ></ImageView>
- <RelativeLayout android:layout_width="fill_parent" android:layout_marginLeft="10dip"
- android:layout_height="40dip">
- <TextView android:id="@+id/tvLabel" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="AppLable : "></TextView>
- <TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content"
- android:layout_toRightOf="@id/tvLabel" android:layout_height="wrap_content"
- android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700"></TextView>
- <TextView android:id="@+id/tvName" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:layout_below="@id/tvLabel"
- android:text="包名:"></TextView>
- <TextView android:id="@+id/tvPkgName" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:layout_below="@id/tvAppLabel"
- android:layout_alignLeft="@id/tvAppLabel" android:textColor="#FFD700"></TextView>
- </RelativeLayout>
- </LinearLayout>
2 、AppInfo.java : 保存应用程序信息的Model类
- /Model类 ,用来存储应用程序信息
- public class AppInfo {
- private String appLabel; //应用程序标签
- private Drawable appIcon ; //应用程序图像
- private Intent intent ; //启动应用程序的Intent ,一般是Action为Main和Category为Lancher的Activity
- private String pkgName ; //应用程序所对应的包名
- public AppInfo(){}
- public String getAppLabel() {
- return appLabel;
- }
- public void setAppLabel(String appName) {
- this.appLabel = appName;
- }
- public Drawable getAppIcon() {
- return appIcon;
- }
- public void setAppIcon(Drawable appIcon) {
- this.appIcon = appIcon;
- }
- public Intent getIntent() {
- return intent;
- }
- public void setIntent(Intent intent) {
- this.intent = intent;
- }
- public String getPkgName(){
- return pkgName ;
- }
- public void setPkgName(String pkgName){
- this.pkgName=pkgName ;
- }
- }
3、 BrowseApplicationInfoAdapter.java : 自定义适配器类,为ListView提供视图
- //自定义适配器类,提供给listView的自定义view
- public class BrowseApplicationInfoAdapter extends BaseAdapter {
- private List<AppInfo> mlistAppInfo = null;
- LayoutInflater infater = null;
- public BrowseApplicationInfoAdapter(Context context, List<AppInfo> apps) {
- infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mlistAppInfo = apps ;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- System.out.println("size" + mlistAppInfo.size());
- return mlistAppInfo.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return mlistAppInfo.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public View getView(int position, View convertview, ViewGroup arg2) {
- System.out.println("getView at " + position);
- View view = null;
- ViewHolder holder = null;
- if (convertview == null || convertview.getTag() == null) {
- view = infater.inflate(R.layout.browse_app_item, null);
- holder = new ViewHolder(view);
- view.setTag(holder);
- }
- else{
- view = convertview ;
- holder = (ViewHolder) convertview.getTag() ;
- }
- AppInfo appInfo = (AppInfo) getItem(position);
- holder.appIcon.setImageDrawable(appInfo.getAppIcon());
- holder.tvAppLabel.setText(appInfo.getAppLabel());
- holder.tvPkgName.setText(appInfo.getPkgName());
- return view;
- }
- class ViewHolder {
- ImageView appIcon;
- TextView tvAppLabel;
- TextView tvPkgName;
- public ViewHolder(View view) {
- this.appIcon = (ImageView) view.findViewById(R.id.imgApp);
- this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);
- this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
- }
- }
- }
