Android杂谈--获取系统及应用程序(PackageManager)

PackageManager是个非常好的东西,其他的详细的细节等日后有时间整理

PackageManager的功能:

•安装,卸载应用
•查询permission相关信息
•查询Application相关信息(application,activity,receiver,service,provider及相应属性等)
•查询已安装应用
•增加,删除permission
•清除用户数据、缓存,代码段等

 

我们可以用PackageManager来显示系统安装的应用程序列表或者系统程序列表

废话先不多说

 

AppShowActivity.java

Java代码  收藏代码
  1. package com.loulijun.appshow;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Context;  
  10. import android.content.pm.ApplicationInfo;  
  11. import android.content.pm.PackageInfo;  
  12. import android.content.pm.PackageManager;  
  13. import android.graphics.drawable.Drawable;  
  14. import android.os.Bundle;  
  15. import android.view.LayoutInflater;  
  16. import android.view.View;  
  17. import android.view.ViewGroup;  
  18. import android.widget.ImageView;  
  19. import android.widget.ListView;  
  20. import android.widget.SimpleAdapter;  
  21. import android.widget.TextView;  
  22.   
  23. public class AppShowActivity extends Activity {  
  24.     ListView lv;  
  25.     MyAdapter adapter;  
  26.     ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();  
  27.       
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.           
  33.         lv = (ListView)findViewById(R.id.lv);  
  34.         //得到PackageManager对象  
  35.         PackageManager pm = getPackageManager();  
  36.         //得到系统安装的所有程序包的PackageInfo对象  
  37.         //List<ApplicationInfo> packs = pm.getInstalledApplications(0);  
  38.         List<PackageInfo> packs = pm.getInstalledPackages(0);  
  39.           
  40.         for(PackageInfo pi:packs)  
  41.         {  
  42.             HashMap<String, Object> map = new HashMap<String, Object>();  
  43.             //显示用户安装的应用程序,而不显示系统程序  
  44. //          if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0&&  
  45. //                  (pi.applicationInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)==0)  
  46. //          {  
  47. //              //这将会显示所有安装的应用程序,包括系统应用程序  
  48. //              map.put("icon", pi.applicationInfo.loadIcon(pm));//图标  
  49. //              map.put("appName", pi.applicationInfo.loadLabel(pm));//应用程序名称  
  50. //              map.put("packageName", pi.applicationInfo.packageName);//应用程序包名  
  51. //              //循环读取并存到HashMap中,再增加到ArrayList上,一个HashMap就是一项  
  52. //              items.add(map);  
  53. //          }  
  54.             //这将会显示所有安装的应用程序,包括系统应用程序  
  55.             map.put("icon", pi.applicationInfo.loadIcon(pm));//图标  
  56.             map.put("appName", pi.applicationInfo.loadLabel(pm));//应用程序名称  
  57.             map.put("packageName", pi.applicationInfo.packageName);//应用程序包名  
  58.             //循环读取并存到HashMap中,再增加到ArrayList上,一个HashMap就是一项  
  59.             items.add(map);  
  60.         }  
  61.         /** 
  62.          * 参数:Context 
  63.          * ArrayList(item的集合) 
  64.          * item的layout 
  65.          * 包含ArrayList中的HashMap的key的数组 
  66.          * key所对应的值的相应的控件id 
  67.          */  
  68.         adapter = new MyAdapter(this, items, R.layout.piitem,   
  69.                 new String[]{"icon""appName""packageName"},  
  70.                 new int[]{R.id.icon, R.id.appName, R.id.packageName});  
  71.         lv.setAdapter(adapter);  
  72.     }  
  73. }  
  74.   
  75. class MyAdapter extends SimpleAdapter  
  76. {  
  77.     private int[] appTo;  
  78.     private String[] appFrom;  
  79.     private ViewBinder appViewBinder;  
  80.     private List<? extends Map<String, ?>>  appData;  
  81.     private int appResource;  
  82.     private LayoutInflater appInflater;  
  83.       
  84.     public MyAdapter(Context context, List<? extends Map<String, ?>> data,  
  85.             int resource, String[] from, int[] to) {  
  86.         super(context, data, resource, from, to);  
  87.         appData = data;  
  88.         appResource = resource;  
  89.         appFrom = from;  
  90.         appTo = to;  
  91.         appInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  92.     }  
  93.       
  94.     public View getView(int position, View convertView, ViewGroup parent)  
  95.     {  
  96.         return createViewFromResource(position, convertView, parent, appResource);  
  97.           
  98.     }  
  99.       
  100.     private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource)  
  101.     {  
  102.         View v;  
  103.         if(convertView == null)  
  104.         {  
  105.             v = appInflater.inflate(resource, parent,false);  
  106.             final int[] to = appTo;  
  107.             final int count = to.length;  
  108.             final View[] holder = new View[count];  
  109.               
  110.             for(int i = 0; i < count; i++)  
  111.             {  
  112.                 holder[i] = v.findViewById(to[i]);  
  113.             }  
  114.             v.setTag(holder);  
  115.         }else  
  116.         {  
  117.             v = convertView;  
  118.         }  
  119.         bindView(position, v);  
  120.         return v;     
  121.     }  
  122.       
  123.     private void bindView(int position, View view)  
  124.     {  
  125.         final Map dataSet = appData.get(position);  
  126.         if(dataSet == null)  
  127.         {  
  128.             return;  
  129.         }  
  130.           
  131.         final ViewBinder binder = appViewBinder;  
  132.         final View[] holder = (View[])view.getTag();  
  133.         final String[] from = appFrom;  
  134.         final int[] to = appTo;  
  135.         final int count = to.length;  
  136.           
  137.         for(int i = 0; i < count; i++)  
  138.         {  
  139.             final View v = holder[i];  
  140.             if(v != null)  
  141.             {  
  142.                 final Object data = dataSet.get(from[i]);  
  143.                 String text = data == null ? "":data.toString();  
  144.                 if(text == null)  
  145.                 {  
  146.                     text = "";  
  147.                 }  
  148.                   
  149.                 boolean bound = false;  
  150.                 if(binder != null)  
  151.                 {  
  152.                     bound = binder.setViewValue(v, data, text);  
  153.                 }  
  154.                   
  155.                 if(!bound)  
  156.                 {  
  157.                     /** 
  158.                      * 自定义适配器,关在在这里,根据传递过来的控件以及值的数据类型, 
  159.                      * 执行相应的方法,可以根据自己需要自行添加if语句。另外,CheckBox等 
  160.                      * 集成自TextView的控件也会被识别成TextView,这就需要判断值的类型 
  161.                      */  
  162.                     if(v instanceof TextView)  
  163.                     {  
  164.                         //如果是TextView控件,则调用SimpleAdapter自带的方法,设置文本  
  165.                         setViewText((TextView)v, text);  
  166.                     }else if(v instanceof ImageView)  
  167.                     {  
  168.                         //如果是ImageView控件,调用自己写的方法,设置图片  
  169.                         setViewImage((ImageView)v, (Drawable)data);  
  170.                     }else  
  171.                     {  
  172.                         throw new IllegalStateException(v.getClass().getName() + " is not a " +  
  173.                                 "view that can be bounds by this SimpleAdapter");  
  174.                     }  
  175.                 }  
  176.             }  
  177.         }  
  178.     }  
  179.     public void setViewImage(ImageView v, Drawable value)  
  180.     {  
  181.         v.setImageDrawable(value);  
  182.     }  
  183. }  

 main.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/lv" android:layout_width="fill_parent"  
  6.         android:layout_height="fill_parent">  
  7.     </ListView>  
  8. </LinearLayout>  

 

piitem.xml

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <ImageView android:id="@+id/icon" android:layout_width="48dip"  
  6.         android:layout_height="48dip" />  
  7.     <LinearLayout android:orientation="vertical"  
  8.         android:layout_width="fill_parent" android:layout_height="wrap_content">  
  9.         <TextView android:id="@+id/appName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>  
  10.         <TextView android:id="@+id/packageName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>  
  11.     </LinearLayout>  
  12. </LinearLayout>  

 现在看看效果图

如果要只是显示用户安装的应用程序的话,可以直接使用


如果用户要显示包括系统程序的话,需要取出if的判断部分,上面代码里面有标识


原文地址:点击打开链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值