你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中获取应用程序(包)的信息-----PackageManager的使用(一)

[日期:2012-02-02] 来源:Linux社区  作者:qinjuning [字体:大 中 小]

二、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

[java]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">>  
  5.     <ListView android:id="@+id/listviewApp" android:layout_width="fill_parent"  
  6.         android:layout_height="fill_parent" ></ListView>  
  7. </LinearLayout>  

browse_app_item.xmlbrowse_app_item.xml

[java]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="50dip">  
  4.   
  5.     <ImageView android:id="@+id/imgApp" android:layout_width="wrap_content"  
  6.         android:layout_height="fill_parent" ></ImageView>  
  7.     <RelativeLayout android:layout_width="fill_parent"  android:layout_marginLeft="10dip"  
  8.         android:layout_height="40dip">  
  9.         <TextView android:id="@+id/tvLabel" android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content" android:text="AppLable : "></TextView>  
  11.         <TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content"  
  12.             android:layout_toRightOf="@id/tvLabel" android:layout_height="wrap_content"  
  13.             android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700"></TextView>  
  14.         <TextView android:id="@+id/tvName" android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content" android:layout_below="@id/tvLabel"   
  16.             android:text="包名:"></TextView>  
  17.         <TextView android:id="@+id/tvPkgName" android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content" android:layout_below="@id/tvAppLabel"  
  19.             android:layout_alignLeft="@id/tvAppLabel" android:textColor="#FFD700"></TextView>  
  20.     </RelativeLayout>  
  21. </LinearLayout>  

    

2 、AppInfo.java : 保存应用程序信息的Model类

[java]
  1. /Model类 ,用来存储应用程序信息  
  2. public class AppInfo {  
  3.     
  4.     private String appLabel;    //应用程序标签   
  5.     private Drawable appIcon ;  //应用程序图像   
  6.     private Intent intent ;     //启动应用程序的Intent ,一般是Action为Main和Category为Lancher的Activity   
  7.     private String pkgName ;    //应用程序所对应的包名   
  8.       
  9.     public AppInfo(){}  
  10.       
  11.     public String getAppLabel() {  
  12.         return appLabel;  
  13.     }  
  14.     public void setAppLabel(String appName) {  
  15.         this.appLabel = appName;  
  16.     }  
  17.     public Drawable getAppIcon() {  
  18.         return appIcon;  
  19.     }  
  20.     public void setAppIcon(Drawable appIcon) {  
  21.         this.appIcon = appIcon;  
  22.     }  
  23.     public Intent getIntent() {  
  24.         return intent;  
  25.     }  
  26.     public void setIntent(Intent intent) {  
  27.         this.intent = intent;  
  28.     }  
  29.     public String getPkgName(){  
  30.         return pkgName ;  
  31.     }  
  32.     public void setPkgName(String pkgName){  
  33.         this.pkgName=pkgName ;  
  34.     }  
  35. }  

 3、 BrowseApplicationInfoAdapter.java : 自定义适配器类,为ListView提供视图

[java]
  1. //自定义适配器类,提供给listView的自定义view   
  2. public class BrowseApplicationInfoAdapter extends BaseAdapter {  
  3.       
  4.     private List<AppInfo> mlistAppInfo = null;  
  5.       
  6.     LayoutInflater infater = null;  
  7.       
  8.     public BrowseApplicationInfoAdapter(Context context,  List<AppInfo> apps) {  
  9.         infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  10.         mlistAppInfo = apps ;  
  11.     }  
  12.     @Override  
  13.     public int getCount() {  
  14.         // TODO Auto-generated method stub   
  15.         System.out.println("size" + mlistAppInfo.size());  
  16.         return mlistAppInfo.size();  
  17.     }  
  18.     @Override  
  19.     public Object getItem(int position) {  
  20.         // TODO Auto-generated method stub   
  21.         return mlistAppInfo.get(position);  
  22.     }  
  23.     @Override  
  24.     public long getItemId(int position) {  
  25.         // TODO Auto-generated method stub   
  26.         return 0;  
  27.     }  
  28.     @Override  
  29.     public View getView(int position, View convertview, ViewGroup arg2) {  
  30.         System.out.println("getView at " + position);  
  31.         View view = null;  
  32.         ViewHolder holder = null;  
  33.         if (convertview == null || convertview.getTag() == null) {  
  34.             view = infater.inflate(R.layout.browse_app_item, null);  
  35.             holder = new ViewHolder(view);  
  36.             view.setTag(holder);  
  37.         }   
  38.         else{  
  39.             view = convertview ;  
  40.             holder = (ViewHolder) convertview.getTag() ;  
  41.         }  
  42.         AppInfo appInfo = (AppInfo) getItem(position);  
  43.         holder.appIcon.setImageDrawable(appInfo.getAppIcon());  
  44.         holder.tvAppLabel.setText(appInfo.getAppLabel());  
  45.         holder.tvPkgName.setText(appInfo.getPkgName());  
  46.         return view;  
  47.     }  
  48.   
  49.     class ViewHolder {  
  50.         ImageView appIcon;  
  51.         TextView tvAppLabel;  
  52.         TextView tvPkgName;  
  53.   
  54.         public ViewHolder(View view) {  
  55.             this.appIcon = (ImageView) view.findViewById(R.id.imgApp);  
  56.             this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);  
  57.             this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);  
  58.         }  
  59.     }  
  60. }  
linux
相关资讯       Android源码  Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款