天天看点

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

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

PackageManager的功能:

•安装,卸载应用

•查询permission相关信息

•查询Application相关信息(application,activity,receiver,service,provider及相应属性等)

•查询已安装应用

•增加,删除permission

•清除用户数据、缓存,代码段等

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

废话先不多说

AppShowActivity.java

Java代码  

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

 main.xml

Java代码  

Android杂谈--获取系统及应用程序(PackageManager)
  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代码  

Android杂谈--获取系统及应用程序(PackageManager)
  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>  

 现在看看效果图

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

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

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

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

原文地址:点击打开链接