在這個例子中將展示如何擷取Application的幾個基本屬性資訊:名字、Package標籤、版本資訊和表徵圖資訊。
建立一個用於儲存基本資料的Class:
Java代碼
- class
PInfo {
-
-
- private
String appname = ""
;
-
- private
String pname = ""
;
-
- private
String versionName = ""
;
-
- private
int
versionCode = 0
;
-
- private
Drawable icon;
-
- private
void
prettyPrint() {
-
- log(appname + "t"
+ pname + "t"
+ versionName + "t"
+ versionCode + "t"
);
-
- }
-
- }
class PInfo { private String appname = ""; private String pname = ""; private String versionName = ""; private int versionCode = 0; private Drawable icon; private void prettyPrint() { log(appname + "t" + pname + "t" + versionName + "t" + versionCode + "t"); } }
接下來是擷取資訊的主體:
Java代碼
- private
ArrayList < PInfo > getInstalledApps(boolean
getSysPackages) {
-
- ArrayList < PInfo > res = new
ArrayList < PInfo > ();
-
- List < PackageInfo > packs = getPackageManager().getInstalledPackages
(0
);
-
- for
(int
i=0
;i < packs.size();i++) {
-
- PackageInfo p = packs.get(i);
-
- if
((!getSysPackages) && (p.versionName == null
)) {
-
- continue
;
-
- }
-
- PInfo newInfo = new
PInfo();
-
- newInfo.appname =
p.applicationInfo.loadLabel
(getPackageManager()).toString();
-
- newInfo.pname = p.packageName;
-
- newInfo.versionName = p.versionName;
-
- newInfo.versionCode = p.versionCode;
-
- newInfo.icon = p.applicationInfo.loadIcon
(getPackageManager());
-
- res.add(newInfo);
-
- }
-
- return
res;
-
- }
private ArrayList < PInfo > getInstalledApps(boolean getSysPackages) { ArrayList < PInfo > res = new ArrayList < PInfo > (); List < PackageInfo > packs = getPackageManager().getInstalledPackages(0); for(int i=0;i < packs.size();i++) { PackageInfo p = packs.get(i); if ((!getSysPackages) && (p.versionName == null)) { continue ; } PInfo newInfo = new PInfo(); newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString(); newInfo.pname = p.packageName; newInfo.versionName = p.versionName; newInfo.versionCode = p.versionCode; newInfo.icon = p.applicationInfo.loadIcon(getPackageManager()); res.add(newInfo); } return res; }
用這個方法來擷取系統內所包含的Applications資訊:
Java代碼
- private
void
listPackages() {
-
- ArrayList < PInfo > apps = getInstalledApps(false
);
-
- final
int
max = apps.size();
-
- for
(int
i=0
; i < max; i++) {
-
- apps.get(i).prettyPrint();
-
- }
-
- }
private void listPackages() { ArrayList < PInfo > apps = getInstalledApps(false); final int max = apps.size(); for (int i=0; i < max; i++) { apps.get(i).prettyPrint(); } }
還有一個相對比較簡單的應用,藉由PackageManager來擷取Application資訊:
Java代碼
- PackageManager manager = this
.getPackageManager
();
-
- try
{
-
- PackageInfo info = manager.getPackageInfo
(this
.getPackageName(), 0
);
-
- String packageName = info.packageName;
-
- int
versionCode = info.versionCode;
-
- String versionName = info.versionName;
-
-
- } catch
(NameNotFoundException e) {
-
- // TODO Auto-generated catch block
-
- }
- http://blog.sina.com.cn/s/blog_6d8189930100nyds.html
final Context context = mContext;
- final PackageManager pm = context.getPackageManager();
abstract
PackageManager
getPackageManager
()
Return
PackageManager instance to find global package information.
擷取系統內
Applications
的基本資料可以使用以下這個方法。
getPackageManager().getInstalledPackages
(0)
- abstract
PackageInfo
| |
getPackageInfo
(String packageName, int flags)Retrieve overall information about an application package that is installed on the system. |
- http://firefox7.javaeye.com/blog/865027