life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks

                                                                

                                                                                                                              转载请注明出处:http://blog.csdn.net/qinjuning

     

          本节内容是如何获取Android系统中应用程序的信息,主要包括packagename、label、icon、占用大小等。具体分为两个

 部分,计划如下:

            第一部分: 获取应用程序的packagename、label、icon等 ;

            第二部分: 获取应用程序的占用大小,包括:缓存大小(cachsize)、数据大小(datasize)。

 

       每部分都为您准备了简单丰富的实例,您一定不会错过。

 

      Android系统为我们提供了很多服务管理的类,包括ActivityManager、PowerManager(电源管理)、AudioManager(音频管理)

等。除此之外,还提供了一个PackageManger管理类,它的主要职责是管理应用程序包。 通过它,我们就可以获取应用程序信息。

 

     引入: AnroidManifest.xml文件节点说明:

    

一、相关类的介绍

 

    PackageItemInfo类

          说明: AndroidManifest.xml文件中所有节点的基类,提供了这些节点的基本信息:a label、icon、 meta-data。它并不

     直接使用,而是由子类继承然后调用相应方法。

          常用字段

               public int icon           获得该资源图片在R文件中的值 (对应于android:icon属性)

               public int labelRes     获得该label在R文件中的值(对应于android:label属性)

               public String name   获得该节点的name值 (对应于android:name属性)

               public String packagename   获得该应用程序的包名 (对应于android:packagename属性)

         常用方法

              Drawable  loadIcon(PackageManager pm)               获得当前应用程序的图像

              CharSequence  loadLabel(PackageManager pm)     获得当前应用程序的label

 

   ActivityInfo类  继承自 PackageItemInfo

          说明: 获得应用程序中<activity/>或者 <receiver />节点的信息 。我们可以通过它来获取我们设置的任何属性,包括

      theme 、launchMode、launchmode等

             常用方法继承至PackageItemInfo类中的loadIcon()和loadLabel() 

 

   ServiceInfo 类

          说明: 同ActivityInfo类似 ,同样继承自 PackageItemInfo,只不过它表示的是<service>节点信息。

 

   ApplicationInfo类 继承自  PackageItemInfo

         说明:获取一个特定引用程序中<application>节点的信息。

         字段说明

      flags字段: FLAG_SYSTEM 系统应用程序

                   FLAG_EXTERNAL_STORAGE 表示该应用安装在sdcard中

         常用方法继承至PackageItemInfo类中的loadIcon()和loadLabel()

 

  ResolveInfo类

        说明:根据<intent>节点来获取其上一层目录的信息,通常是<activity>、<receiver>、<service>节点信息。

       常用字段

             public  ActivityInfo  activityInfo     获取 ActivityInfo对象,即<activity>或<receiver >节点信息

             public ServiceInfo   serviceInfo     获取 ServiceInfo对象,即<activity>节点信息

       常用方法 

             Drawable loadIcon(PackageManager pm)             获得当前应用程序的图像

             CharSequence loadLabel(PackageManager pm)  获得当前应用程序的label

 

 PackageInfo类

      说明:手动获取AndroidManifest.xml文件的信息 。

       常用字段

           public String    packageName                   包名

           public ActivityInfo[]     activities                   所有<activity>节点信息

           public ApplicationInfo applicationInfo       <application>节点信息,只有一个

           public ActivityInfo[]    receivers                  所有<receiver>节点信息,多个

           public ServiceInfo[]    services                  所有<service>节点信息 ,多个

 

PackageManger 类

      说明: 获得已安装的应用程序信息 。可以通过getPackageManager()方法获得。

      常用方法

          public abstract PackageManager  getPackageManager()   

               功能:获得一个PackageManger对象

         public abstrac  tDrawable    getApplicationIcon(StringpackageName)

               参数: packageName 包名

               功能:返回给定包名的图标,否则返回null

 

       public abstract ApplicationInfo   getApplicationInfo(String packageName, int flags)

 

               参数:packagename 包名

                           flags 该ApplicationInfo是此flags标记,通常可以直接赋予常数0即可

               功能:返回该ApplicationInfo对象

 

          public abstract List<ApplicationInfo>  getInstalledApplications(int flags)

               参数:flag为一般为GET_UNINSTALLED_PACKAGES,那么此时会返回所有ApplicationInfo。我们可以对ApplicationInfo

                     的flags过滤,得到我们需要的。

               功能:返回给定条件的所有PackageInfo

 

          public abstract List<PackageInfo>  getInstalledPackages(int flags) 

             参数如上

             功能:返回给定条件的所有PackageInfo

 

       public abstractResolveInfo  resolveActivity(Intent intent, int flags)

            参数:  intent 查寻条件,Activity所配置的action和category

                          flags: MATCH_DEFAULT_ONLY    :Category必须带有CATEGORY_DEFAULT的Activity,才匹配

                                      GET_INTENT_FILTERS         :匹配Intent条件即可

                                                  GET_RESOLVED_FILTER    :匹配Intent条件即可

            功能 :返回给定条件的ResolveInfo对象(本质上是Activity) 

 

       public abstract  List<ResolveInfo>  queryIntentActivities(Intent intent, int flags)

            参数同上

            功能 :返回给定条件的所有ResolveInfo对象(本质上是Activity),集合对象

 

      public abstract ResolveInfo  resolveService(Intent intent, int flags)

           参数同上

           功能 :返回给定条件的ResolveInfo对象(本质上是Service)

 

     public abstract List<ResolveInfoqueryIntentServices(Intent intent, int flags)

          参数同上

          功能 :返回给定条件的所有ResolveInfo对象(本质上是Service),集合对象

 

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

  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

  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类

  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提供视图

  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. }  

 

4 、MainActivity.java 主工程逻辑 

          请仔细体会queryIntentActivities()方法,并且注意到排序,它很重要。

  1. <span style="font-size:13px;">public class MainActivity extends Activity implements OnItemClickListener {  
  2.   
  3.     private ListView listview = null;  
  4.   
  5.     private List<AppInfo> mlistAppInfo = null;  
  6.   
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.browse_app_list);  
  11.   
  12.         listview = (ListView) findViewById(R.id.listviewApp);  
  13.         mlistAppInfo = new ArrayList<AppInfo>();  
  14.         queryAppInfo(); // 查询所有应用程序信息  
  15.         BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(  
  16.                 this, mlistAppInfo);  
  17.         listview.setAdapter(browseAppAdapter);  
  18.         listview.setOnItemClickListener(this);  
  19.     }  
  20.     // 点击跳转至该应用程序  
  21.     public void onItemClick(AdapterView<?> arg0, View view, int position,  
  22.             long arg3) {  
  23.         // TODO Auto-generated method stub  
  24.         Intent intent = mlistAppInfo.get(position).getIntent();  
  25.         startActivity(intent);  
  26.     }  
  27.     // 获得所有启动Activity的信息,类似于Launch界面  
  28.     public void queryAppInfo() {  
  29.         PackageManager pm = this.getPackageManager(); // 获得PackageManager对象  
  30.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  31.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
  32.         // 通过查询,获得所有ResolveInfo对象.  
  33.         List<ResolveInfo> resolveInfos = pm  
  34.                 .queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY);  
  35.         // 调用系统排序 , 根据name排序  
  36.         // 该排序很重要,否则只能显示系统应用,而不能列出第三方应用程序  
  37.         Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));  
  38.         if (mlistAppInfo != null) {  
  39.             mlistAppInfo.clear();  
  40.             for (ResolveInfo reInfo : resolveInfos) {  
  41.                 String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name  
  42.                 String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名  
  43.                 String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label  
  44.                 Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标  
  45.                 // 为应用程序的启动Activity 准备Intent  
  46.                 Intent launchIntent = new Intent();  
  47.                 launchIntent.setComponent(new ComponentName(pkgName,  
  48.                         activityName));  
  49.                 // 创建一个AppInfo对象,并赋值  
  50.                 AppInfo appInfo = new AppInfo();  
  51.                 appInfo.setAppLabel(appLabel);  
  52.                 appInfo.setPkgName(pkgName);  
  53.                 appInfo.setAppIcon(icon);  
  54.                 appInfo.setIntent(launchIntent);  
  55.                 mlistAppInfo.add(appInfo); // 添加至列表中  
  56.                 System.out.println(appLabel + " activityName---" + activityName  
  57.                         + " pkgName---" + pkgName);  
  58.             }  
  59.         }  
  60.     }  
  61. }</span>  


 好了,第一个Demo完成 。。 

 

 Demo 2:

        demo2在布局、适配器方面和Demo1一样。只是利用了getInstalledApplications()方法,继而通过ApplicationInfo.flags来挑选

  我们希望的ApplicationInfo对象。

       图:

                       

 

 过滤应用程序如下:

  1. package com.qiner.appinfo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.List;  
  6.   
  7. import com.qiner.appinfo.R;  
  8.   
  9. import android.app.Activity;  
  10. import android.app.Application;  
  11. import android.content.pm.ApplicationInfo;  
  12. import android.content.pm.PackageManager;  
  13. import android.os.Bundle;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17. import android.widget.ListView;  
  18.   
  19. public class MainActivity extends Activity  {  
  20.   
  21.     public static final int FILTER_ALL_APP = 0; // 所有应用程序  
  22.     public static final int FILTER_SYSTEM_APP = 1; // 系统程序  
  23.     public static final int FILTER_THIRD_APP = 2; // 第三方应用程序  
  24.     public static final int FILTER_SDCARD_APP = 3; // 安装在SDCard的应用程序  
  25.   
  26.     private ListView listview = null;  
  27.   
  28.     private PackageManager pm;  
  29.     private int filter = FILTER_ALL_APP;   
  30.     private List<AppInfo> mlistAppInfo ;  
  31.     private BrowseApplicationInfoAdapter browseAppAdapter = null ;  
  32.     /** Called when the activity is first created. */  
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.browse_app_list);  
  37.         listview = (ListView) findViewById(R.id.listviewApp);  
  38.         if(getIntent()!=null){  
  39.             filter = getIntent().getIntExtra("filter", 0) ;  
  40.         }  
  41.         mlistAppInfo = queryFilterAppInfo(filter); // 查询所有应用程序信息  
  42.         // 构建适配器,并且注册到listView  
  43.         browseAppAdapter = new BrowseApplicationInfoAdapter(this, mlistAppInfo);  
  44.         listview.setAdapter(browseAppAdapter);  
  45.     }  
  46.     // 根据查询条件,查询特定的ApplicationInfo  
  47.     private List<AppInfo> queryFilterAppInfo(int filter) {  
  48.         pm = this.getPackageManager();  
  49.         // 查询所有已经安装的应用程序  
  50.         List<ApplicationInfo> listAppcations = pm  
  51.                 .getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);  
  52.         Collections.sort(listAppcations,  
  53.                 new ApplicationInfo.DisplayNameComparator(pm));// 排序  
  54.         List<AppInfo> appInfos = new ArrayList<AppInfo>(); // 保存过滤查到的AppInfo  
  55.         // 根据条件来过滤  
  56.         switch (filter) {  
  57.         case FILTER_ALL_APP: // 所有应用程序  
  58.             appInfos.clear();  
  59.             for (ApplicationInfo app : listAppcations) {  
  60.                 appInfos.add(getAppInfo(app));  
  61.             }  
  62.             return appInfos;  
  63.         case FILTER_SYSTEM_APP: // 系统程序  
  64.             appInfos.clear();  
  65.             for (ApplicationInfo app : listAppcations) {  
  66.                 if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {  
  67.                     appInfos.add(getAppInfo(app));  
  68.                 }  
  69.             }  
  70.             return appInfos;  
  71.         case FILTER_THIRD_APP: // 第三方应用程序  
  72.             appInfos.clear();  
  73.             for (ApplicationInfo app : listAppcations) {  
  74.                 //非系统程序  
  75.                 if ((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {  
  76.                     appInfos.add(getAppInfo(app));  
  77.                 }   
  78.                 //本来是系统程序,被用户手动更新后,该系统程序也成为第三方应用程序了  
  79.                 else if ((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){  
  80.                     appInfos.add(getAppInfo(app));  
  81.                 }  
  82.             }  
  83.             break;  
  84.         case FILTER_SDCARD_APP: // 安装在SDCard的应用程序  
  85.             appInfos.clear();  
  86.             for (ApplicationInfo app : listAppcations) {  
  87.                 if ((app.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {  
  88.                     appInfos.add(getAppInfo(app));  
  89.                 }  
  90.             }  
  91.             return appInfos;  
  92.         default:  
  93.             return null;  
  94.         }  
  95.         return appInfos;  
  96.     }  
  97.     // 构造一个AppInfo对象 ,并赋值  
  98.     private AppInfo getAppInfo(ApplicationInfo app) {  
  99.         AppInfo appInfo = new AppInfo();  
  100.         appInfo.setAppLabel((String) app.loadLabel(pm));  
  101.         appInfo.setAppIcon(app.loadIcon(pm));  
  102.         appInfo.setPkgName(app.packageName);  
  103.         return appInfo;  
  104.     }  
  105. }  



    你可以在此基础上,构建更多丰富的应用。比说说Settings模块中的卸载安装应用程序等。

posted on 2012-03-19 16:42 life02 阅读(1056) 评论(0)  编辑 收藏 引用 所属分类: Android开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理