ApplicationInfo 的进一步学习 – 热爱改变生活
我的GitHub GitHub |     登录
  • If you can't fly, then run; if you can't run, then walk; if you can't walk, then crawl
  • but whatever you do, you have to keep moving forward。
  • “你骗得了我有什么用,这是你自己的人生”
  • 曾有伤心之地,入梦如听 此歌

ApplicationInfo 的进一步学习

Android sinvader 4134℃ 0评论

AndroidManifest.xml 采用 xml 文件格式描述,使用 xml 标签的形式描述每个属性,AndroidManifest.xml 的解析也遵循 xml 解析的规律,对 AndroidManifest.xml 的解析主要用到 PackageManager,PackageInfo,ApplicationInfo 三个类,PackageInfo 描述了应用级的信息,包括应用的版本号,版本名称,对应 AndroidManifest.xml 最外层的信息,ApplicationInfo 描述了标签之间的信息,包括声明的 meta-data 和 activity 信息。下面我做了一个小 demo 来测试下如何获得里面的值:
首先是要获得的 Manifest 的文件:

[sourcecode language=”java”]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.packagemanagertest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="18" />
<compatible-screens >
<screen android:screenSize="normal"/>
<screen android:screenDensity="hdpi"/>
</compatible-screens>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.NFC"/>
<application
android:manageSpaceActivity="com.example.packagemanagertest.MainActivity"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" android:name="TestApplication">
<activity
android:name="com.example.packagemanagertest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
[/sourcecode]

然后是主要的 Activity:MainActivity

[sourcecode language=”java”]
package com.example.packagemanagertest;

import java.util.List;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
private String TAG = "Test";
private PackageManager pm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pm = getPackageManager();
ApplicationTool tool=new ApplicationTool(this);
String str=tool.getDataDir();
System.out.println(str);
System.out.println(tool.getIcon());
System.out.println(tool.getProcessName());
System.out.println(tool.getTargetSdkVersion());
System.out.println(tool.getTheme());
System.out.println(tool.getVersionCode());
System.out.println(tool.getVersionName());
String[] strs = tool.getPermissions();
for (String string : strs) {
System.out.println(string);
}

List<ApplicationInfo> list1 = pm.getInstalledApplications(128);
if (list1.size() != 0) {
for (ApplicationInfo info : list1) {
if ("com.example.packagemanagertest".equals(info.packageName)) {

// 配置文件中的 android:backupAgent 属性值, 用于备份
String backupAgentName = info.backupAgentName;
Log.i(TAG, "【配置文件中的 android:backupAgent 属性值, 用于备份–backupAgentName:】" + backupAgentName);
// 获取继承 Application 类的对象, 维护全局的 Application 状态
// 但一般都不用继承的方式,可以通过 Context.getApplicationContext() 方法得到
String className = info.className;
Log.i(TAG, "【获取继承自 Application 类的对象–className:】" + className);
// 包名
String packageName = info.packageName;
Log.i(TAG, "【应用的包名–packageName:】" + packageName);
// <android:manageSpaceActivity> 标签的值
// 该标签代表了指定一个 activity 来处理用户的空间数据。而不是通过设置里的一个按钮来操作
String activityName = info.manageSpaceActivityName;
Log.i(TAG, "【<android:manageSpaceActivity> 标签的值–manageSpaceActivityName:】" + activityName);
// 应用所在的进程
String processName = info.processName;
Log.i(TAG, "【应用所在的进程–processName:】" + processName);
// 权限
String permisson = info.permission;
Log.i(TAG, "【权限–permisson:】" + permisson);
// 通过其他方法获得权限列表
try {
String[] permisson2 = getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;
for (int i = 0; i < permisson2.length; i++) {
Log.i(TAG, "【权限列表–:】" + permisson2[i]);
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// 创建对象时,传入的是 GET_SHARED_LIBRARY_FILES 该属性才有值
String[] files = info.sharedLibraryFiles;
Log.i(TAG, "【如果 getPackageManager().getInstalledApplications(128) 中传入的值为 GET_SHARED_LIBRARY_FILES 该属性才有值–sharedLibraryFiles:】" + files);
// 存放数据的路径
String dataPath = info.dataDir;
Log.i(TAG, "【存放数据的路径–dataDir:】" + dataPath);
// 本地路径
String nativePath = info.nativeLibraryDir;
Log.i(TAG, "【本地路径–nativeLibraryDir:】" + nativePath);
// 公共资源路径
String punlicSourcePath = info.publicSourceDir;
Log.i(TAG, "【公共资源路径–publicSourceDir:】" + punlicSourcePath);
// 资源路径
String sourcePath = info.sourceDir;
Log.i(TAG, "【资源路径–sourcePath:】" + sourcePath);
// 内同的 activity 的 task 名称
String taskAffinity = info.taskAffinity;
Log.i(TAG, "【taskAffinity:】" + taskAffinity);
// 如果是 false, 代表 application 里的所有组件都禁用
boolean enable = info.enabled;
Log.i(TAG, "【false, 代表 application 里的所有组件都禁用–enable:】" + enable);
// 表述资源文件的标识
int descriRes = info.descriptionRes;
Log.i(TAG, "【表述资源文件的标识–descriptionRes:】" + descriRes);
int flag = info.flags;
Log.i(TAG, "【flag:】" + flag);
// 指定 smallest screen width 的值,超过这个值,就要开启屏幕兼容
int compatibleWidth = info.compatibleWidthLimitDp;// android:compatibleWidthLimitDp 属性
Log.i(TAG, "【超过这个值,就要开启屏幕兼容–compatibleWidthLimitDp:】" + compatibleWidth);
// 同上,只是这时候用户无法禁止屏幕兼容模式,说明是强制启动屏幕兼容
int largestWidth = info.largestWidthLimitDp;// android:largestWidthLimitDp 属性
Log.i(TAG, "【强制启动屏幕兼容–largestWidthLimitDp:】" + largestWidth);
// 所需屏幕空间的最短尺寸,
int samllestWidth = info.requiresSmallestWidthDp;// android:requiresSmallestWidthDp 属性
Log.i(TAG, "【屏幕空间的最短尺寸,–requiresSmallestWidthDp:】" + samllestWidth);
// 应用所需的最小 sdk 版本
int sdkVersion = info.targetSdkVersion;
Log.i(TAG, "【最小 sdk 版本–targetSdkVersion:】" + sdkVersion);
int theme = info.theme;
Log.i(TAG, "【android:theme 标签–theme:】" + theme);// android:theme=
int uid = info.uid;
Log.i(TAG, "【uid: " + uid);
// 配置文件中的 uiOptions 属性的值
int uiOptions = info.uiOptions;
Log.i(TAG, "【uiOptions 属性的值–uiOptions:】" + uiOptions);
}
}
}
}
}

[/sourcecode]

之后是一个提取出来的工具类:ApplicationTool

[sourcecode language=”java”]
package com.example.packagemanagertest;

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

/**
* @Title: ApplicationTool.java
* @Package com.example.packagemanagertest
* @Description:
* @author wudkj.com
* @date 2015-4-19 上午 12:41:16
* @version V1.0
*/
public class ApplicationTool {
private PackageInfo packageInfo;
private PackageManager pkgmanager;
private String packageName;
private Context mContext;
private ApplicationInfo info = null;

public ApplicationTool(Context mContext) {
this.mContext=mContext;
pkgmanager = mContext.getPackageManager();
packageName = mContext.getPackageName();
try {
packageInfo = pkgmanager.getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}

/**
* @return 版本号
*/
public int getVersionCode() {
return packageInfo.versionCode;
}

/**
* @return 版本名称
*/
public String getVersionName() {
return packageInfo.versionName;
}

/**
* @return 当前程序的权限列表
*/
public String[] getPermissions() {
if (info == null) {
List<ApplicationInfo> list1 = pkgmanager.getInstalledApplications(128);
if (list1.size() != 0) {
for (ApplicationInfo infoTmp : list1) {
if (infoTmp == null) {
return null;
}
if (packageName.equals(infoTmp.packageName)) {
info = infoTmp;
try {
return mContext.getPackageManager().getPackageInfo(info.packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
}
}
} else {
try {
return mContext.getPackageManager().getPackageInfo(info.packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
return null;
}

/**
* @return 存放数据的路径
*/
public String getDataDir() {
if (info == null) {
List<ApplicationInfo> list1 = pkgmanager.getInstalledApplications(128);
if (list1.size() != 0) {
for (ApplicationInfo infoTmp : list1) {
if (infoTmp == null) {
return "";
}
if (packageName.equals(infoTmp.packageName)) {
info = infoTmp;
return info.dataDir;
}
}
}
} else {
return info.dataDir;
}
return "";
}

/**
* @return 最高 sdk 版本
*/
public int getTargetSdkVersion() {
if (info == null) {
List<ApplicationInfo> list1 = pkgmanager.getInstalledApplications(128);
if (list1.size() != 0) {
for (ApplicationInfo infoTmp : list1) {
if (packageName.equals(infoTmp.packageName)) {
info = infoTmp;
return info.targetSdkVersion;
}
}
}
} else {
return info.targetSdkVersion;
}
return -1;
}

/**
* @return 程序图标
*/
public int getIcon() {
if (info == null) {
List<ApplicationInfo> list1 = pkgmanager.getInstalledApplications(128);
if (list1.size() != 0) {
for (ApplicationInfo infoTmp : list1) {
if (packageName.equals(infoTmp.packageName)) {
info = infoTmp;
return info.icon;
}
}
}
} else {
return info.icon;
}
return -1;
}

/**
* @return 程序的主题
*/
public int getTheme() {
if (info == null) {
List<ApplicationInfo> list1 = pkgmanager.getInstalledApplications(128);
if (list1.size() != 0) {
for (ApplicationInfo infoTmp : list1) {
if (packageName.equals(infoTmp.packageName)) {
info = infoTmp;
return info.theme;
}
}
}
} else {
return info.theme;
}
return -1;
}

/**
* @return 程序的进程名称
*/
public String getProcessName() {
if (info == null) {
List<ApplicationInfo> list1 = pkgmanager.getInstalledApplications(128);
if (list1.size() != 0) {
for (ApplicationInfo infoTmp : list1) {
if (packageName.equals(infoTmp.packageName)) {
info = infoTmp;
return info.processName;
}
}
}
} else {
return info.processName;
}
return "";
}
}

[/sourcecode]

下面是运行工具类获得的数据:

[sourcecode language=”java”]
04-19 01:06:27.477: I/System.out(11502): /data/data/com.example.packagemanagertest
04-19 01:06:27.477: I/System.out(11502): 2130837504
04-19 01:06:27.477: I/System.out(11502): com.example.packagemanagertest
04-19 01:06:27.477: I/System.out(11502): 18
04-19 01:06:27.477: I/System.out(11502): 2131099649
04-19 01:06:27.477: I/System.out(11502): 1
04-19 01:06:27.477: I/System.out(11502): 1.0
04-19 01:06:27.477: I/System.out(11502): android.permission.READ_SMS
04-19 01:06:27.477: I/System.out(11502): android.permission.INTERNET
04-19 01:06:27.477: I/System.out(11502): android.permission.NFC
[/sourcecode]

然后是下面 log 类型获得的数据:

[sourcecode language=”java”]
04-19 01:06:27.507: I/Test(11502):【配置文件中的 android:backupAgent 属性值, 用于备份–backupAgentName:】null
04-19 01:06:27.507: I/Test(11502):【获取继承自 Application 类的对象–className:】com.example.packagemanagertest.TestApplication
04-19 01:06:27.507: I/Test(11502):【应用的包名–packageName:】com.example.packagemanagertest
04-19 01:06:27.507: I/Test(11502):【<android:manageSpaceActivity> 标签的值–manageSpaceActivityName:】com.example.packagemanagertest.MainActivity
04-19 01:06:27.507: I/Test(11502):【应用所在的进程–processName:】com.example.packagemanagertest
04-19 01:06:27.517: I/Test(11502):【权限–permisson:】null
04-19 01:06:27.517: I/Test(11502):【权限列表–:】android.permission.READ_SMS
04-19 01:06:27.517: I/Test(11502):【权限列表–:】android.permission.INTERNET
04-19 01:06:27.517: I/Test(11502):【权限列表–:】android.permission.NFC
04-19 01:06:27.517: I/Test(11502):【如果 getPackageManager().getInstalledApplications(128) 中传入的值为 GET_SHARED_LIBRARY_FILES 该属性才有值–sharedLibraryFiles:】null
04-19 01:06:27.517: I/Test(11502):【存放数据的路径–dataDir:】/data/data/com.example.packagemanagertest
04-19 01:06:27.517: I/Test(11502):【本地路径–nativeLibraryDir:】/data/app-lib/com.example.packagemanagertest-1
04-19 01:06:27.517: I/Test(11502):【公共资源路径–publicSourceDir:】/data/app/com.example.packagemanagertest-1.apk
04-19 01:06:27.517: I/Test(11502):【资源路径–sourcePath:】/data/app/com.example.packagemanagertest-1.apk
04-19 01:06:27.517: I/Test(11502):【taskAffinity:】com.example.packagemanagertest
04-19 01:06:27.517: I/Test(11502):【false, 代表 application 里的所有组件都禁用–enable:】true
04-19 01:06:27.517: I/Test(11502):【表述资源文件的标识–descriptionRes:】0
04-19 01:06:27.517: I/Test(11502):【flag:】8961606
04-19 01:06:27.517: I/Test(11502):【超过这个值,就要开启屏幕兼容–compatibleWidthLimitDp:】0
04-19 01:06:27.517: I/Test(11502):【强制启动屏幕兼容–largestWidthLimitDp:】0
04-19 01:06:27.517: I/Test(11502):【屏幕空间的最短尺寸,–requiresSmallestWidthDp:】0
04-19 01:06:27.517: I/Test(11502):【最小 sdk 版本–targetSdkVersion:】18
04-19 01:06:27.517: I/Test(11502):【android:theme 标签–theme:】2131099649
04-19 01:06:27.517: I/Test(11502):【uid: 10080
04-19 01:06:27.517: I/Test(11502):【uiOptions 属性的值–uiOptions:】0
[/sourcecode]

有点乱,不过有源码,地址:http://pan.baidu.com/s/1eQivVVG

¥ 有帮助么?打赏一下~

转载请注明:热爱改变生活.cn » ApplicationInfo 的进一步学习


本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » ApplicationInfo 的进一步学习

喜欢 (2)
发表我的评论
取消评论
表情

如需邮件形式接收回复,请注册登录

Hi,你需要填写昵称和邮箱~

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址