Android中獲取應用程式(包)的資訊-----PackageManager的使用(一)
轉載請註明出處:http://blog.csdn.net/qinjuning
本節內容是如何獲取Android系統中應用程式的資訊,主要包括packagename、label、icon、佔用大小等。具體分為兩個
部分,計劃如下:
第一部分: 獲取應用程式的packagename、label、icon等 ;
第二部分: 獲取應用程式的佔用大小,包括:快取大小(cachsize)、資料大小(datasize)。
每部分都為您準備了簡單豐富的例項,您一定不會錯過。
Android系統為我們提供了很多服務管理的類,包括ActivityManager、PowerManager(電源管理)、AudioManager(音訊管理)
等。除此之外,還提供了一個PackageManger管理類,它的主要職責是管理應用程式包。 通過它,我們就可以獲取應用程式資訊。
引入: AnroidManifest.xml檔案節點說明:
一、相關類的介紹
PackageItemInfo類
說明: AndroidManifest.xml檔案中所有節點的基類,提供了這些節點的基本資訊:a label、icon、 meta-data。它並不
直接使用,而是由子類繼承然後呼叫相應方法。
常用欄位:
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<ResolveInfo> queryIntentServices(Intent intent, int flags)
引數同上
功能 :返回給定條件的所有ResolveInfo物件(本質上是Service),集合物件
二、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
- <?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
- <?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類
- /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 ;
- }
- }
3、 BrowseApplicationInfoAdapter.java : 自定義介面卡類,為ListView提供檢視
- //自定義介面卡類,提供給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()方法,並且注意到排序,它很重要。
- <span style="font-size:13px;">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);
- }
- }
- }
- }</span>
好了,第一個Demo完成 。。
Demo 2:
demo2在佈局、介面卡方面和Demo1一樣。只是利用了getInstalledApplications()方法,繼而通過ApplicationInfo.flags來挑選
我們希望的ApplicationInfo物件。
圖:
過濾應用程式如下:
- 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
相關文章
- Android中獲取應用程式(包)的大小-----PackageManager的使用(二)AndroidPackage
- Android中獲取系統記憶體資訊以及程式資訊-----ActivityManager的使用(一)Android記憶體
- Android獲取應用基本資訊Android
- 【Android PackageManager】使用PackageManager讀取APK ICON時候的大坑AndroidPackageAPK
- android 5.0以上獲取前臺應用的包名Android
- 通過 PackageManager 獲得你想要的 App 資訊PackageAPP
- Linux應用程式獲取執行緒棧的資訊Linux執行緒
- MFC中獲取程式自身的版本資訊
- Android-獲取app包資訊異常AndroidAPP
- 【Android】使用PackageManager讀取APK ICON時候的大坑AndroidPackageAPK
- Linux應用程式設計:用一種討巧方式,來獲取執行緒棧的使用資訊Linux程式設計執行緒
- Flutter獲取IOS bundle id和Android應用包名FlutteriOSAndroid
- Hover獲取Android使用者輸入的機密資訊(一)Android
- Web應用中的普通java程式獲取資原始檔WebJava
- Android中獲取當前位置資訊Android
- 如何優雅獲的獲取不同系統版本中的程式資訊
- SAP中關於使用者IP資訊的獲取
- Android逆向之旅---Android中如何獲取在非Root裝置中獲取應用隱私資料Android
- 在Java中獲取Android端登陸的裝置資訊JavaAndroid
- 讓應用程式從IE中獲取鍵入的網址 (轉)
- 微信小程式 獲取使用者資訊微信小程式
- AWR 中 top sql 的資訊獲取 - 分析SQL
- jProcesses:使用Java獲取跨平臺程式的詳細資訊Java
- 微信小程式三種獲取使用者資訊的方式微信小程式
- 在ASP.NET MVC應用程式中隨機獲取一個字串ASP.NETMVC隨機字串
- Android系統資訊獲取Android
- Hover獲取Android使用者輸入的機密資訊(二)Android
- 使用BeautifulSoap爬取安智網的所有應用資訊
- 微信小程式獲取使用者資訊方法微信小程式
- Android逆向之旅---爆破資訊類應用「最右」的防抓包策略Android
- Android中獲取正在執行的應用程式-----ActivityManager.RunningAppProcessInfo類詳解(二)AndroidAPP
- MySQL 如何獲取執行中的Queries資訊?MySql
- android典型程式碼系列(二十四)------獲取某個應用程式所佔用的記憶體Android記憶體
- Android獲取手機配置資訊Android
- android GPS 獲取城市資訊Android
- 使用PHP獲取影像檔案的EXIF資訊PHP
- API介面在電商商品資料獲取中的應用API
- Android應用鎖之獲取棧頂ActivityAndroid