本节内容是如何获取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对象,即<service>节点信息

常用方法

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<ResolveInfo> queryIntentServices(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

[java] view plaincopyprint?
  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>
<?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

[java] view plaincopyprint?
  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. <ImageView android:id="@+id/imgApp" android:layout_width="wrap_content"
  5. android:layout_height="fill_parent" ></ImageView>
  6. <RelativeLayout android:layout_width="fill_parent"  android:layout_marginLeft="10dip"
  7. android:layout_height="40dip">
  8. <TextView android:id="@+id/tvLabel" android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" android:text="AppLable : "></TextView>
  10. <TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content"
  11. android:layout_toRightOf="@id/tvLabel" android:layout_height="wrap_content"
  12. android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700"></TextView>
  13. <TextView android:id="@+id/tvName" android:layout_width="wrap_content"
  14. android:layout_height="wrap_content" android:layout_below="@id/tvLabel"
  15. android:text="包名:"></TextView>
  16. <TextView android:id="@+id/tvPkgName" android:layout_width="wrap_content"
  17. android:layout_height="wrap_content" android:layout_below="@id/tvAppLabel"
  18. android:layout_alignLeft="@id/tvAppLabel" android:textColor="#FFD700"></TextView>
  19. </RelativeLayout>
  20. </LinearLayout>
<?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类

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

[java] view plaincopyprint?
  1. //自定义适配器类,提供给listView的自定义view
  2. public class BrowseApplicationInfoAdapter extends BaseAdapter {
  3. private List<AppInfo> mlistAppInfo = null;
  4. LayoutInflater infater = null;
  5. public BrowseApplicationInfoAdapter(Context context,  List<AppInfo> apps) {
  6. infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  7. mlistAppInfo = apps ;
  8. }
  9. @Override
  10. public int getCount() {
  11. // TODO Auto-generated method stub
  12. System.out.println("size" + mlistAppInfo.size());
  13. return mlistAppInfo.size();
  14. }
  15. @Override
  16. public Object getItem(int position) {
  17. // TODO Auto-generated method stub
  18. return mlistAppInfo.get(position);
  19. }
  20. @Override
  21. public long getItemId(int position) {
  22. // TODO Auto-generated method stub
  23. return 0;
  24. }
  25. @Override
  26. public View getView(int position, View convertview, ViewGroup arg2) {
  27. System.out.println("getView at " + position);
  28. View view = null;
  29. ViewHolder holder = null;
  30. if (convertview == null || convertview.getTag() == null) {
  31. view = infater.inflate(R.layout.browse_app_item, null);
  32. holder = new ViewHolder(view);
  33. view.setTag(holder);
  34. }
  35. else{
  36. view = convertview ;
  37. holder = (ViewHolder) convertview.getTag() ;
  38. }
  39. AppInfo appInfo = (AppInfo) getItem(position);
  40. holder.appIcon.setImageDrawable(appInfo.getAppIcon());
  41. holder.tvAppLabel.setText(appInfo.getAppLabel());
  42. holder.tvPkgName.setText(appInfo.getPkgName());
  43. return view;
  44. }
  45. class ViewHolder {
  46. ImageView appIcon;
  47. TextView tvAppLabel;
  48. TextView tvPkgName;
  49. public ViewHolder(View view) {
  50. this.appIcon = (ImageView) view.findViewById(R.id.imgApp);
  51. this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);
  52. this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
  53. }
  54. }
  55. }
//自定义适配器类,提供给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 ;}@Overridepublic int getCount() {// TODO Auto-generated method stubSystem.out.println("size" + mlistAppInfo.size());return mlistAppInfo.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn mlistAppInfo.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic 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);}}
}

4 、MainActivity.java 主工程逻辑

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

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

好了,第一个Demo完成 。。

 

 Demo 2:

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

我们希望的ApplicationInfo对象。

图:

         

过滤应用程序如下:

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

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

   本节的源代码已上传,下载地址:http://download.csdn.net/detail/qinjuning/3775869

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

  1. android中获取应用程序(包)的信息,Android中获取应用程序(包)的信息PackageManager的使用(一).doc...

    Android中获取应用程序(包)的信息PackageManager的使用(一) 本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等 ...

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

    转载:http://blog.csdn.net/qinjuning/article/details/6867806 本节内容是如何获取Android系统中应用程序的信息,主要包括packagename ...

  3. Android之获取应用程序(包)的大小-----PackageManager的使用(二)

    http://blog.csdn.net/qinjuning/article/details/6892054 通过第一部分 << Android中获取应用程序(包)的信息-----Pack ...

  4. Android中获取手机内所有应用信息

    有时候我们开发中需要获取到手机中安装的所有软件相关信息,比如判断是否安装了某个软件,或者获取某个软件的具体包名等.这里我贴出相关方法. 首先我们要有一个APP的实体类 public class App ...

  5. android获取应用权限管理,如何在android中获取应用程序权限设置?

    我正在开发一个小应用程序,它只列出那些连接到互联网的应用程序.如何从Packageinfo类中读取 android清单文件以编程方式访问每个应用程序的权限设置? private void getWeb ...

  6. Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用

    关于PackageManager和ActivityManager的使用 ,自己也写了一些DEMO 了,基本上写的线路参考了Settings模块下的 应用程序,大家如果真正的有所兴趣,建议大家看看源码, ...

  7. android apk 的信息,Android中获取apk基本信息

    一 PackageManager可以获得的所有包节点信息: 1,所有节点的基类:PackageItemInfo: 2,PackageInfo:package的全面信息,与AndroidManifest ...

  8. android 获取 第三方应用,​android PackageManager的使用如何获取第三方应用程序(包)的信息,求大神指点...

    我只写出了系统的应用程序(包)的信息,第三方的没有出来,怎么办,求解 public class MainActivity extends Activity { List appInfos; @Over ...

  9. android获取运行应用程序,Android中获取正在运行的应用程序

    Android中获取正在运行的应用程序 ActivityMain.java 1.public class ActivityMain extends ListActivity { 2. @Overrid ...

最新文章

  1. linux的tar中ztvf,linux中的tar命令(2)
  2. Spring复习笔记:3
  3. 重磅!革命级AI代码补全工具,这款撸码利器让程序员界沸腾了!
  4. 别在迷恋正则表达式解析html了,好吗?
  5. 计算力即生产力,智算中心就是智慧时代的“发电厂” | MEET2021
  6. TeamViewer 14 on Nvidia Jetson TX2
  7. 乐视手机权限开启方法
  8. 版本控制工具Git 常用操作命令
  9. RTSP协议基本分析
  10. python nlp_Python NLP入门教程
  11. Linux学习总结(18)——Linux使用init命令关机、重启、切换模式
  12. 气门组的结构组成有哪些_挖机破碎钳子,液压粉碎钳都有哪些结构组成,你知道吗...
  13. Keras:框架架构
  14. APP社交类源代码分享直接上车
  15. 「Golang」sync.Once用法以及源码讲解
  16. 2013菏泽国际牡丹文化旅游节
  17. Java设计模式——行为型模式之观察者模式
  18. 牛逼的python代码_几段牛逼的 Python 代码理解面向对象
  19. php中的ol标签,html ol标签的使用与定义详解
  20. 滤镜艺术---新海诚(你的名字)滤镜特效的实现解密

热门文章

  1. Go 语言实现 23 种设计模式适配器
  2. 分布式是写出来的(六)
  3. C语言malloc和calloc的区别
  4. linux中的dup和fcntl的用法
  5. linux字符驱动之点亮LED
  6. 字符串的规范使用(二)
  7. 比特币源码学习笔记(一)
  8. 7年老Android一次操蛋的面试经历,深度好文
  9. 如何将.crt的ssl证书文件转换成.pem格式
  10. setdefault()方法