天天看點

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)

原文位址:點選打開連結