PackageManager是個非常好的東西,其他的詳細的細節等日後有時間整理
PackageManager的功能:
•安裝,解除安裝應用
•查詢permission相關資訊
•查詢Application相關資訊(application,activity,receiver,service,provider及相應屬性等)
•查詢已安裝應用
•增加,删除permission
•清除使用者資料、緩存,代碼段等
我們可以用PackageManager來顯示系統安裝的應用程式清單或者系統程式清單
廢話先不多說
AppShowActivity.java
Java代碼

- package com.loulijun.appshow;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.content.Context;
- import android.content.pm.ApplicationInfo;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.TextView;
- public class AppShowActivity extends Activity {
- ListView lv;
- MyAdapter adapter;
- ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- lv = (ListView)findViewById(R.id.lv);
- //得到PackageManager對象
- PackageManager pm = getPackageManager();
- //得到系統安裝的所有程式包的PackageInfo對象
- //List<ApplicationInfo> packs = pm.getInstalledApplications(0);
- List<PackageInfo> packs = pm.getInstalledPackages(0);
- for(PackageInfo pi:packs)
- {
- HashMap<String, Object> map = new HashMap<String, Object>();
- //顯示使用者安裝的應用程式,而不顯示系統程式
- // if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0&&
- // (pi.applicationInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)==0)
- // {
- // //這将會顯示所有安裝的應用程式,包括系統應用程式
- // map.put("icon", pi.applicationInfo.loadIcon(pm));//圖示
- // map.put("appName", pi.applicationInfo.loadLabel(pm));//應用程式名稱
- // map.put("packageName", pi.applicationInfo.packageName);//應用程式包名
- // //循環讀取并存到HashMap中,再增加到ArrayList上,一個HashMap就是一項
- // items.add(map);
- // }
- //這将會顯示所有安裝的應用程式,包括系統應用程式
- map.put("icon", pi.applicationInfo.loadIcon(pm));//圖示
- map.put("appName", pi.applicationInfo.loadLabel(pm));//應用程式名稱
- map.put("packageName", pi.applicationInfo.packageName);//應用程式包名
- //循環讀取并存到HashMap中,再增加到ArrayList上,一個HashMap就是一項
- items.add(map);
- }
- adapter = new MyAdapter(this, items, R.layout.piitem,
- new String[]{"icon", "appName", "packageName"},
- new int[]{R.id.icon, R.id.appName, R.id.packageName});
- lv.setAdapter(adapter);
- }
- }
- class MyAdapter extends SimpleAdapter
- {
- private int[] appTo;
- private String[] appFrom;
- private ViewBinder appViewBinder;
- private List<? extends Map<String, ?>> appData;
- private int appResource;
- private LayoutInflater appInflater;
- public MyAdapter(Context context, List<? extends Map<String, ?>> data,
- int resource, String[] from, int[] to) {
- super(context, data, resource, from, to);
- appData = data;
- appResource = resource;
- appFrom = from;
- appTo = to;
- appInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- public View getView(int position, View convertView, ViewGroup parent)
- {
- return createViewFromResource(position, convertView, parent, appResource);
- }
- private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource)
- {
- View v;
- if(convertView == null)
- {
- v = appInflater.inflate(resource, parent,false);
- final int[] to = appTo;
- final int count = to.length;
- final View[] holder = new View[count];
- for(int i = 0; i < count; i++)
- {
- holder[i] = v.findViewById(to[i]);
- }
- v.setTag(holder);
- }else
- {
- v = convertView;
- }
- bindView(position, v);
- return v;
- }
- private void bindView(int position, View view)
- {
- final Map dataSet = appData.get(position);
- if(dataSet == null)
- {
- return;
- }
- final ViewBinder binder = appViewBinder;
- final View[] holder = (View[])view.getTag();
- final String[] from = appFrom;
- final int[] to = appTo;
- final int count = to.length;
- for(int i = 0; i < count; i++)
- {
- final View v = holder[i];
- if(v != null)
- {
- final Object data = dataSet.get(from[i]);
- String text = data == null ? "":data.toString();
- if(text == null)
- {
- text = "";
- }
- boolean bound = false;
- if(binder != null)
- {
- bound = binder.setViewValue(v, data, text);
- }
- if(!bound)
- {
- if(v instanceof TextView)
- {
- //如果是TextView控件,則調用SimpleAdapter自帶的方法,設定文本
- setViewText((TextView)v, text);
- }else if(v instanceof ImageView)
- {
- //如果是ImageView控件,調用自己寫的方法,設定圖檔
- setViewImage((ImageView)v, (Drawable)data);
- }else
- {
- throw new IllegalStateException(v.getClass().getName() + " is not a " +
- "view that can be bounds by this SimpleAdapter");
- }
- }
- }
- }
- }
- public void setViewImage(ImageView v, Drawable value)
- {
- v.setImageDrawable(value);
- }
- }
main.xml
Java代碼

- <?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/lv" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- </ListView>
- </LinearLayout>
piitem.xml
Java代碼

- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView android:id="@+id/icon" android:layout_width="48dip"
- android:layout_height="48dip" />
- <LinearLayout android:orientation="vertical"
- android:layout_width="fill_parent" android:layout_height="wrap_content">
- <TextView android:id="@+id/appName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
- <TextView android:id="@+id/packageName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
- </LinearLayout>
- </LinearLayout>
現在看看效果圖
如果要隻是顯示使用者安裝的應用程式的話,可以直接使用
如果使用者要顯示包括系統程式的話,需要取出if的判斷部分,上面代碼裡面有辨別
原文位址:點選打開連結