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

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

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

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

  我们希望的ApplicationInfo对象。

       图:

                       

 

 过滤应用程序如下:

[java]
  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模块中的卸载安装应用程序等。 

linux
相关资讯       Android源码  Android开发教程 
本文评论  ���查看全部评论 (0)
表情: 表情 姓名: 字数

       

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