PackageManager–擷取應用程式資訊

來源:互聯網
上載者:User
android.content.pm包裡的類都是管理應用程式的。

 

擷取已安裝應用程式資訊

通過Activity的getPackageManager()方法可以得到PackageManager的對象

1.PackageManager 的getInstalledPackages(int flags)方法可以得到所有安裝在機器上的程式的包資訊類對象List<PackageInfo>,PackageInfo類中有一值applicationInfo可以得到Application的對象。

2.PackageManager的getInstalledApplications(int flags)方法可以得到所有安裝在機器上的程式的application對象List<ApplicationInfo>;

 

 Application類繼承類PackageItemInfo,在PackageItemInfo類中有方法loadIcon(PackageManager
pm)返回應用程式圖示Drawable類型。有方法loadLabel(PackageManager
pm)返回應用程式名稱,CharSequence類型。

 

  ApplicationInfo中無法擷取版本號碼和版本名。PackageInfo類中有包名、版本名、版本號碼。

 

  如果只需要安裝程式的名字和圖片而不需要版本號碼和版本名資訊,用方法2擷取ApplicationInfo。如果都需要,用方法1擷取PackageInfo,再通過PackageInfo擷取ApplicationInfo。

 

    PackageItemInfo中有值

public int icon   :  A drawable resource identifier (in the package's resources) of this component's icon.
public int labelRes   :  A string resource identifier (in the package's resources) of this component's label.

是int型,基本用不上。

 

public
String
packageName     :  Name of the package that this item is in.

這個是程式的包名,也就是說,可以在ApplicationInfo對象中得到安裝程式的圖片名字和包名。版本號碼和版本名只能在PackageInfo中獲得。ApplicationInfo與PackageInfo中都能得到包名!

 

           程式名和圖片還可以通過PackageManager類獲得,PackageManager有方法getApplicationIcon(String
packageName)和getApplicationIcon(ApplicationInfo
info)都可以獲得應用程式的圖片,getApplicationLabel(ApplicationInfo
info)方法可以獲得應用程式的名字。

 

下面這段代碼可以得到所有已安裝應用啟動程式的Activity的資訊,即Main Activity Info:

PackageManager pm = getPackageManager();        Intent filter = new Intent(Intent.ACTION_MAIN);    filter.addCategory(Intent.CATEGORY_LAUNCHER);    List<ResolveInfo>list = pm.queryIntentActivities(filter, PackageManager.GET_RESOLVED_FILTER);

啟動手機裡的第1個程式:

    ComponentName cn =new ComponentName(list.get(0).activityInfo.packageName, list.get(0).activityInfo.name);    Intent intent = new Intent(Intent.ACTION_MAIN);    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);    intent.setComponent(cn);    startActivity(intent);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.