Android中獲取應用程序(包)的信息-----PackageManager的使用

 部分,計劃以下:java

            第一部分: 獲取應用程序的packagename、label、icon等 ;android

            第二部分: 獲取應用程序的佔用大小,包括:緩存大小(cachsize)、數據大小(datasize)。windows

 

       每部分都爲您準備了簡單豐富的實例,您必定不會錯過。緩存

 

      Android系統爲咱們提供了不少服務管理的類,包括ActivityManager、PowerManager(電源管理)、AudioManager(音頻管理)app

等。除此以外,還提供了一個PackageManger管理類,它的主要職責是管理應用程序包。 經過它,咱們就能夠獲取應用程序信息。ide

 

     引入: AnroidManifest.xml文件節點說明:佈局

    

1、相關類的介紹

 

    PackageItemInfo類

          說明: AndroidManifest.xml文件中全部節點的基類,提供了這些節點的基本信息:a label、icon、 meta-data。它並不this

     直接使用,而是由子類繼承而後調用相應方法。spa

          經常使用字段

               public int icon           得到該資源圖片在R文件中的值 (對應於android:icon屬性)

               public int labelRes     得到該label在R文件中的值(對應於android:label屬性)

               public String name   得到該節點的name值 (對應於android:name屬性)

               public String packagename   得到該應用程序的包名 (對應於android:packagename屬性)

        經常使用方法

              Drawable  loadIcon(PackageManager pm)               得到當前應用程序的圖像

              CharSequence  loadLabel(PackageManager pm)     得到當前應用程序的label

 

   ActivityInfo類  繼承自 PackageItemInfo

          說明: 得到應用程序中<activity/>或者 <receiver />節點的信息 。咱們能夠經過它來獲取咱們設置的任何屬性,包括

      theme 、launchMode、launchmode等

             經常使用方法繼承至PackageItemInfo類中的loadIcon()和loadLabel() 

 

   ServiceInfo 類

          說明: 同ActivityInfo相似 ,一樣繼承自 PackageItemInfo,只不過它表示的是<service>節點信息。

 

   ApplicationInfo類 繼承自  PackageItemInfo

         說明:獲取一個特定引用程序中<application>節點的信息。

         字段說明

      flags字段: FLAG_SYSTEM 系統應用程序

                   FLAG_EXTERNAL_STORAGE 表示該應用安裝在sdcard中

         經常使用方法繼承至PackageItemInfo類中的loadIcon()和loadLabel()

 

  ResolveInfo類

        說明:根據<intent>節點來獲取其上一層目錄的信息,一般是<activity>、<receiver>、<service>節點信息。

       經常使用字段

             public  ActivityInfo  activityInfo     獲取 ActivityInfo對象,即<activity>或<receiver >節點信息

             public ServiceInfo   serviceInfo     獲取 ServiceInfo對象,即<service>節點信息

       經常使用方法 

             Drawable loadIcon(PackageManager pm)             得到當前應用程序的圖像

             CharSequence loadLabel(PackageManager pm)  得到當前應用程序的label

 

 PackageInfo類

       說明:手動獲取AndroidManifest.xml文件的信息 。

       經常使用字段

           public String    packageName                   包名

           public ActivityInfo[]     activities                   全部<activity>節點信息

           public ApplicationInfo applicationInfo       <application>節點信息,只有一個

           public ActivityInfo[]    receivers                  全部<receiver>節點信息,多個

           public ServiceInfo[]    services                  全部<service>節點信息 ,多個

 

PackageManger 類

      說明: 得到已安裝的應用程序信息 。能夠經過getPackageManager()方法得到。

      經常使用方法

          public abstract PackageManager  getPackageManager()   

               功能:得到一個PackageManger對象

         public abstrac  tDrawable    getApplicationIcon(StringpackageName)

               參數: packageName 包名

               功能:返回給定包名的圖標,不然返回null

 

       public abstract ApplicationInfo   getApplicationInfo(String packageName, int flags)

 

               參數:packagename 包名

                           flags 該ApplicationInfo是此flags標記,一般能夠直接賦予常數0便可

               功能:返回該ApplicationInfo對象

 

          public abstract List<ApplicationInfo>  getInstalledApplications(int flags)

               參數:flag爲通常爲GET_UNINSTALLED_PACKAGES,那麼此時會返回全部ApplicationInfo。咱們能夠對ApplicationInfo

                     的flags過濾,獲得咱們須要的。

               功能:返回給定條件的全部PackageInfo

 

          public abstract List<PackageInfo>  getInstalledPackages(int flags) 

             參數如上

             功能:返回給定條件的全部PackageInfo

 

       public abstractResolveInfo  resolveActivity(Intent intent, int flags)

            參數:  intent 查尋條件,Activity所配置的action和category

                          flags: MATCH_DEFAULT_ONLY    :Category必須帶有CATEGORY_DEFAULT的Activity,才匹配

                                      GET_INTENT_FILTERS         :匹配Intent條件便可

                                                  GET_RESOLVED_FILTER    :匹配Intent條件便可

            功能 :返回給定條件的ResolveInfo對象(本質上是Activity) 

 

       public abstract  List<ResolveInfo>  queryIntentActivities(Intent intent, int flags)

            參數同上

            功能 :返回給定條件的全部ResolveInfo對象(本質上是Activity),集合對象

 

      public abstract ResolveInfo  resolveService(Intent intent, int flags)

           參數同上

           功能 :返回給定條件的ResolveInfo對象(本質上是Service)

 

     public abstract List<ResolveInfoqueryIntentServices(Intent intent, int flags)

          參數同上

          功能 :返回給定條件的全部ResolveInfo對象(本質上是Service),集合對象

 

2、DEMO講解

 

            經過前面的介紹,相信您必定很瞭解了,本質上來說,這些XXXInfo類不過是咱們在AndroidManifest.XML文件中定義的信息,

知道到這點了,理解起來就不是很難了。

         下面我透過兩個簡答的DEMO,來學以至用。

           Demo 1: 經過queryIntentActivities()方法,查詢Android系統的全部具有ACTION_MAIN和CATEGORY_LAUNCHER

      的Intent的應用程序,點擊後,能啓動該應用,說白了就是作一個相似Home程序的簡易Launcher 。

           Demo 2 :經過getInstalledApplications()方法獲取應用,而後對其過濾,查找出咱們須要的第三方應用,系統應用,安裝在sdcard的應用。

 

      Demo1  :

         圖:

              

  1 、佈局文件: 主要有兩個:帶listview的browse_app_list.xml文件 ;listview的項browse_app_item.xml

browse_app_list.xml

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

browse_app_item.xmlbrowse_app_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="50dip">  
  4.   
  5.     <ImageView android:id="@+id/imgApp" android:layout_width="wrap_content"  
  6.         android:layout_height="fill_parent" ></ImageView>  
  7.     <RelativeLayout android:layout_width="fill_parent"  android:layout_marginLeft="10dip"  
  8.         android:layout_height="40dip">  
  9.         <TextView android:id="@+id/tvLabel" android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content" android:text="AppLable : "></TextView>  
  11.         <TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content"  
  12.             android:layout_toRightOf="@id/tvLabel" android:layout_height="wrap_content"  
  13.             android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700"></TextView>  
  14.         <TextView android:id="@+id/tvName" android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content" android:layout_below="@id/tvLabel"   
  16.             android:text="包名:"></TextView>  
  17.         <TextView android:id="@+id/tvPkgName" android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content" android:layout_below="@id/tvAppLabel"  
  19.             android:layout_alignLeft="@id/tvAppLabel" android:textColor="#FFD700"></TextView>  
  20.     </RelativeLayout>  
  21. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="50dip">

	<ImageView android:id="@+id/imgApp" android:layout_width="wrap_content"
		android:layout_height="fill_parent" ></ImageView>
	<RelativeLayout android:layout_width="fill_parent"  android:layout_marginLeft="10dip"
		android:layout_height="40dip">
		<TextView android:id="@+id/tvLabel" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="AppLable : "></TextView>
		<TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content"
			android:layout_toRightOf="@id/tvLabel" android:layout_height="wrap_content"
			android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700"></TextView>
		<TextView android:id="@+id/tvName" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:layout_below="@id/tvLabel" 
			android:text="包名:"></TextView>
		<TextView android:id="@+id/tvPkgName" android:layout_width="wrap_content"
		    android:layout_height="wrap_content" android:layout_below="@id/tvAppLabel"
		    android:layout_alignLeft="@id/tvAppLabel" android:textColor="#FFD700"></TextView>
	</RelativeLayout>
</LinearLayout>

       

2 、AppInfo.java : 保存應用程序信息的Model類

  1. /Model類 ,用來存儲應用程序信息  
  2. public class AppInfo {  
  3.     
  4.     private String appLabel;    //應用程序標籤  
  5.     private Drawable appIcon ;  //應用程序圖像  
  6.     private Intent intent ;     //啓動應用程序的Intent ,通常是Action爲Main和Category爲Lancher的Activity  
  7.     private String pkgName ;    //應用程序所對應的包名  
  8.       
  9.     public AppInfo(){}  
  10.       
  11.     public String getAppLabel() {  
  12.         return appLabel;  
  13.     }  
  14.     public void setAppLabel(String appName) {  
  15.         this.appLabel = appName;  
  16.     }  
  17.     public Drawable getAppIcon() {  
  18.         return appIcon;  
  19.     }  
  20.     public void setAppIcon(Drawable appIcon) {  
  21.         this.appIcon = appIcon;  
  22.     }  
  23.     public Intent getIntent() {  
  24.         return intent;  
  25.     }  
  26.     public void setIntent(Intent intent) {  
  27.         this.intent = intent;  
  28.     }  
  29.     public String getPkgName(){  
  30.         return pkgName ;  
  31.     }  
  32.     public void setPkgName(String pkgName){  
  33.         this.pkgName=pkgName ;  
  34.     }  
  35. }  
/Model類 ,用來存儲應用程序信息
public class AppInfo {
  
	private String appLabel;    //應用程序標籤
	private Drawable appIcon ;  //應用程序圖像
	private Intent intent ;     //啓動應用程序的Intent ,通常是Action爲Main和Category爲Lancher的Activity
	private String pkgName ;    //應用程序所對應的包名
	
	public AppInfo(){}
	
	public String getAppLabel() {
		return appLabel;
	}
	public void setAppLabel(String appName) {
		this.appLabel = appName;
	}
	public Drawable getAppIcon() {
		return appIcon;
	}
	public void setAppIcon(Drawable appIcon) {
		this.appIcon = appIcon;
	}
	public Intent getIntent() {
		return intent;
	}
	public void setIntent(Intent intent) {
		this.intent = intent;
	}
	public String getPkgName(){
		return pkgName ;
	}
	public void setPkgName(String pkgName){
		this.pkgName=pkgName ;
	}
}

 三、 BrowseApplicationInfoAdapter.java : 自定義適配器類,爲ListView提供視圖

  1. //自定義適配器類,提供給listView的自定義view  
  2. public class BrowseApplicationInfoAdapter extends BaseAdapter {  
  3.       
  4.     private List<AppInfo> mlistAppInfo = null;  
  5.       
  6.     LayoutInflater infater = null;  
  7.       
  8.     public BrowseApplicationInfoAdapter(Context context,  List<AppInfo> apps) {  
  9.         infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  10.         mlistAppInfo = apps ;  
  11.     }  
  12.     @Override  
  13.     public int getCount() {  
  14.         // TODO Auto-generated method stub  
  15.         System.out.println("size" + mlistAppInfo.size());  
  16.         return mlistAppInfo.size();  
  17.     }  
  18.     @Override  
  19.     public Object getItem(int position) {  
  20.         // TODO Auto-generated method stub  
  21.         return mlistAppInfo.get(position);  
  22.     }  
  23.     @Override  
  24.     public long getItemId(int position) {  
  25.         // TODO Auto-generated method stub  
  26.         return 0;  
  27.     }  
  28.     @Override  
  29.     public View getView(int position, View convertview, ViewGroup arg2) {  
  30.         System.out.println("getView at " + position);  
  31.         View view = null;  
  32.         ViewHolder holder = null;  
  33.         if (convertview == null || convertview.getTag() == null) {  
  34.             view = infater.inflate(R.layout.browse_app_item, null);  
  35.             holder = new ViewHolder(view);  
  36.             view.setTag(holder);  
  37.         }   
  38.         else{  
  39.             view = convertview ;  
  40.             holder = (ViewHolder) convertview.getTag() ;  
  41.         }  
  42.         AppInfo appInfo = (AppInfo) getItem(position);  
  43.         holder.appIcon.setImageDrawable(appInfo.getAppIcon());  
  44.         holder.tvAppLabel.setText(appInfo.getAppLabel());  
  45.         holder.tvPkgName.setText(appInfo.getPkgName());  
  46.         return view;  
  47.     }  
  48.   
  49.     class ViewHolder {  
  50.         ImageView appIcon;  
  51.         TextView tvAppLabel;  
  52.         TextView tvPkgName;  
  53.   
  54.         public ViewHolder(View view) {  
  55.             this.appIcon = (ImageView) view.findViewById(R.id.imgApp);  
  56.             this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);  
  57.             this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);  
  58.         }  
  59.     }  
  60. }  
//自定義適配器類,提供給listView的自定義view
public class BrowseApplicationInfoAdapter extends BaseAdapter {
	
	private List<AppInfo> mlistAppInfo = null;
	
	LayoutInflater infater = null;
    
	public BrowseApplicationInfoAdapter(Context context,  List<AppInfo> apps) {
		infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mlistAppInfo = apps ;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		System.out.println("size" + mlistAppInfo.size());
		return mlistAppInfo.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mlistAppInfo.get(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public View getView(int position, View convertview, ViewGroup arg2) {
		System.out.println("getView at " + position);
		View view = null;
		ViewHolder holder = null;
		if (convertview == null || convertview.getTag() == null) {
			view = infater.inflate(R.layout.browse_app_item, null);
			holder = new ViewHolder(view);
			view.setTag(holder);
		} 
		else{
			view = convertview ;
			holder = (ViewHolder) convertview.getTag() ;
		}
		AppInfo appInfo = (AppInfo) getItem(position);
		holder.appIcon.setImageDrawable(appInfo.getAppIcon());
		holder.tvAppLabel.setText(appInfo.getAppLabel());
		holder.tvPkgName.setText(appInfo.getPkgName());
		return view;
	}

	class ViewHolder {
		ImageView appIcon;
		TextView tvAppLabel;
		TextView tvPkgName;

		public ViewHolder(View view) {
			this.appIcon = (ImageView) view.findViewById(R.id.imgApp);
			this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);
			this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
		}
	}
}

 

4 、MainActivity.java 主工程邏輯 

          請仔細體會queryIntentActivities()方法,而且注意到排序,它很重要。

  1. <span style="font-size: 13px;">public class MainActivity extends Activity implements OnItemClickListener {  
  2.   
  3.     private ListView listview = null;  
  4.   
  5.     private List<AppInfo> mlistAppInfo = null;  
  6.   
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.browse_app_list);  
  11.   
  12.         listview = (ListView) findViewById(R.id.listviewApp);  
  13.         mlistAppInfo = new ArrayList<AppInfo>();  
  14.         queryAppInfo(); // 查詢全部應用程序信息  
  15.         BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(  
  16.                 this, mlistAppInfo);  
  17.         listview.setAdapter(browseAppAdapter);  
  18.         listview.setOnItemClickListener(this);  
  19.     }  
  20.     // 點擊跳轉至該應用程序  
  21.     public void onItemClick(AdapterView<?> arg0, View view, int position,  
  22.             long arg3) {  
  23.         // TODO Auto-generated method stub  
  24.         Intent intent = mlistAppInfo.get(position).getIntent();  
  25.         startActivity(intent);  
  26.     }  
  27.     // 得到全部啓動Activity的信息,相似於Launch界面  
  28.     public void queryAppInfo() {  
  29.         PackageManager pm = this.getPackageManager(); // 得到PackageManager對象  
  30.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  31.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
  32.         // 經過查詢,得到全部ResolveInfo對象.  
  33.         List<ResolveInfo> resolveInfos = pm  
  34.                 .queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY);  
  35.         // 調用系統排序 , 根據name排序  
  36.         // 該排序很重要,不然只能顯示系統應用,而不能列出第三方應用程序  
  37.         Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));  
  38.         if (mlistAppInfo != null) {  
  39.             mlistAppInfo.clear();  
  40.             for (ResolveInfo reInfo : resolveInfos) {  
  41.                 String activityName = reInfo.activityInfo.name; // 得到該應用程序的啓動Activity的name  
  42.                 String pkgName = reInfo.activityInfo.packageName; // 得到應用程序的包名  
  43.                 String appLabel = (String) reInfo.loadLabel(pm); // 得到應用程序的Label  
  44.                 Drawable icon = reInfo.loadIcon(pm); // 得到應用程序圖標  
  45.                 // 爲應用程序的啓動Activity 準備Intent  
  46.                 Intent launchIntent = new Intent();  
  47.                 launchIntent.setComponent(new ComponentName(pkgName,  
  48.                         activityName));  
  49.                 // 建立一個AppInfo對象,並賦值  
  50.                 AppInfo appInfo = new AppInfo();  
  51.                 appInfo.setAppLabel(appLabel);  
  52.                 appInfo.setPkgName(pkgName);  
  53.                 appInfo.setAppIcon(icon);  
  54.                 appInfo.setIntent(launchIntent);  
  55.                 mlistAppInfo.add(appInfo); // 添加至列表中  
  56.                 System.out.println(appLabel + " activityName---" + activityName  
  57.                         + " pkgName---" + pkgName);  
  58.             }  
  59.         }  
  60.     }  
  61. }</span>  
public class MainActivity extends Activity implements OnItemClickListener {

	private ListView listview = null;

	private List<AppInfo> mlistAppInfo = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browse_app_list);

		listview = (ListView) findViewById(R.id.listviewApp);
		mlistAppInfo = new ArrayList<AppInfo>();
		queryAppInfo(); // 查詢全部應用程序信息
		BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(
				this, mlistAppInfo);
		listview.setAdapter(browseAppAdapter);
		listview.setOnItemClickListener(this);
	}
	// 點擊跳轉至該應用程序
	public void onItemClick(AdapterView<?> arg0, View view, int position,
			long arg3) {
		// TODO Auto-generated method stub
		Intent intent = mlistAppInfo.get(position).getIntent();
		startActivity(intent);
	}
	// 得到全部啓動Activity的信息,相似於Launch界面
	public void queryAppInfo() {
		PackageManager pm = this.getPackageManager(); // 得到PackageManager對象
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		// 經過查詢,得到全部ResolveInfo對象.
		List<ResolveInfo> resolveInfos = pm
				.queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY);
		// 調用系統排序 , 根據name排序
		// 該排序很重要,不然只能顯示系統應用,而不能列出第三方應用程序
		Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));
		if (mlistAppInfo != null) {
			mlistAppInfo.clear();
			for (ResolveInfo reInfo : resolveInfos) {
				String activityName = reInfo.activityInfo.name; // 得到該應用程序的啓動Activity的name
				String pkgName = reInfo.activityInfo.packageName; // 得到應用程序的包名
				String appLabel = (String) reInfo.loadLabel(pm); // 得到應用程序的Label
				Drawable icon = reInfo.loadIcon(pm); // 得到應用程序圖標
				// 爲應用程序的啓動Activity 準備Intent
				Intent launchIntent = new Intent();
				launchIntent.setComponent(new ComponentName(pkgName,
						activityName));
				// 建立一個AppInfo對象,並賦值
				AppInfo appInfo = new AppInfo();
				appInfo.setAppLabel(appLabel);
				appInfo.setPkgName(pkgName);
				appInfo.setAppIcon(icon);
				appInfo.setIntent(launchIntent);
				mlistAppInfo.add(appInfo); // 添加至列表中
				System.out.println(appLabel + " activityName---" + activityName
						+ " pkgName---" + pkgName);
			}
		}
	}
}

 好了,第一個Demo完成 。。 

 

 Demo 2:

        demo2在佈局、適配器方面和Demo1同樣。只是利用了getInstalledApplications()方法,繼而經過ApplicationInfo.flags來挑選

  咱們但願的ApplicationInfo對象。

       圖:

                       

 

 過濾應用程序以下:

  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. }  
package com.qiner.appinfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.qiner.appinfo.R;

import android.app.Activity;
import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity  {

	public static final int FILTER_ALL_APP = 0; // 全部應用程序
	public static final int FILTER_SYSTEM_APP = 1; // 系統程序
	public static final int FILTER_THIRD_APP = 2; // 第三方應用程序
	public static final int FILTER_SDCARD_APP = 3; // 安裝在SDCard的應用程序

	private ListView listview = null;

	private PackageManager pm;
	private int filter = FILTER_ALL_APP; 
	private List<AppInfo> mlistAppInfo ;
	private BrowseApplicationInfoAdapter browseAppAdapter = null ;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browse_app_list);
		listview = (ListView) findViewById(R.id.listviewApp);
		if(getIntent()!=null){
			filter = getIntent().getIntExtra("filter", 0) ;
		}
		mlistAppInfo = queryFilterAppInfo(filter); // 查詢全部應用程序信息
		// 構建適配器,而且註冊到listView
		browseAppAdapter = new BrowseApplicationInfoAdapter(this, mlistAppInfo);
		listview.setAdapter(browseAppAdapter);
	}
	// 根據查詢條件,查詢特定的ApplicationInfo
	private List<AppInfo> queryFilterAppInfo(int filter) {
		pm = this.getPackageManager();
		// 查詢全部已經安裝的應用程序
		List<ApplicationInfo> listAppcations = pm
				.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
		Collections.sort(listAppcations,
				new ApplicationInfo.DisplayNameComparator(pm));// 排序
		List<AppInfo> appInfos = new ArrayList<AppInfo>(); // 保存過濾查到的AppInfo
		// 根據條件來過濾
		switch (filter) {
		case FILTER_ALL_APP: // 全部應用程序
			appInfos.clear();
			for (ApplicationInfo app : listAppcations) {
				appInfos.add(getAppInfo(app));
			}
			return appInfos;
		case FILTER_SYSTEM_APP: // 系統程序
			appInfos.clear();
			for (ApplicationInfo app : listAppcations) {
				if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
					appInfos.add(getAppInfo(app));
				}
			}
			return appInfos;
		case FILTER_THIRD_APP: // 第三方應用程序
			appInfos.clear();
			for (ApplicationInfo app : listAppcations) {
				//非系統程序
				if ((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
					appInfos.add(getAppInfo(app));
				} 
				//原本是系統程序,被用戶手動更新後,該系統程序也成爲第三方應用程序了
				else if ((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){
					appInfos.add(getAppInfo(app));
				}
			}
			break;
		case FILTER_SDCARD_APP: // 安裝在SDCard的應用程序
			appInfos.clear();
			for (ApplicationInfo app : listAppcations) {
				if ((app.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
					appInfos.add(getAppInfo(app));
				}
			}
			return appInfos;
		default:
			return null;
		}
		return appInfos;
	}
	// 構造一個AppInfo對象 ,並賦值
	private AppInfo getAppInfo(ApplicationInfo app) {
		AppInfo appInfo = new AppInfo();
		appInfo.setAppLabel((String) app.loadLabel(pm));
		appInfo.setAppIcon(app.loadIcon(pm));
		appInfo.setPkgName(app.packageName);
		return appInfo;
	}
}

    你能夠在此基礎上,構建更多豐富的應用。比說說Settings模塊中的卸載安裝應用程序等。 

 

 

    本節的源代碼已上傳,下載地址:http://download.csdn.net/detail/qinjuning/3775869

相關文章
相關標籤/搜索