需要的资源
关键jar包:AXMLPrinter2.jar
工具类
import android.util.TypedValue;
import com.hyxy.bean.AppVersionBean;
import org.xmlpull.v1.XmlPullParser;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import android.content.res.AXmlResourceParser;
public class AnalysisApk {
/**
* @param apkUrl apk的文件地址
* @return
*/
public static AppVersionBean getApkInfo(String apkUrl) {
AppVersionBean appVersionBean = new AppVersionBean();
ZipFile zipFile;
System.out.println(apkUrl);
try {
zipFile = new ZipFile(new File(apkUrl));
Enumeration enumeration = zipFile.entries();
ZipEntry zipEntry = null ;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
if (zipEntry.isDirectory()) {
} else {
// 读取从此处开始,之后内容是什么不太明白,可以需要改动的地方用绿色标注
if("AndroidManifest.xml".equals(zipEntry.getName()))
{
try {
AXmlResourceParser parser=new AXmlResourceParser();
parser.open(zipFile.getInputStream(zipEntry));
while (true) {
int type=parser.next();
if (type== XmlPullParser.END_DOCUMENT) { // 如果解析到xml结尾
break;
}
switch (type) {
case XmlPullParser.START_TAG: { // xml的开始标签
for (int i=0;i!=parser.getAttributeCount();++i) {
if ("versionCode".equals(parser.getAttributeName(i))){
//apk.setVersionCode(parser.getAttributeValue(parser,i));
appVersionBean.setVersionCode(getAttributeValue(parser,i));
} else if("versionName".equals(parser.getAttributeName(i))){
//apk.setVersionName(getAttributeValue(parser,i));
appVersionBean.setVersionName(getAttributeValue(parser,i));
} else if ("package".equals(parser.getAttributeName(i))){
//apk.setPackageKey(getAttributeValue(parser,i));
System.out.println("包名"+getAttributeValue(parser,i));
}
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return appVersionBean;
}
private static String getAttributeValue(AXmlResourceParser parser,int index) {
int type=parser.getAttributeValueType(index);
int data=parser.getAttributeValueData(index);
if (type == TypedValue.TYPE_STRING) {
return parser.getAttributeValue(index);
}
if (type == TypedValue.TYPE_ATTRIBUTE) {
return String.format("?%s% 08X", getPackage(data), data); // 此处应为“% 08X”,下同,所有百分号后应无空格
}
if (type == TypedValue.TYPE_REFERENCE) {
return String.format("@%s% 08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_FLOAT) {
return String.valueOf(Float.intBitsToFloat(data));
}
if (type == TypedValue.TYPE_INT_HEX) {
return String.format("0x% 08X", data);
}
if (type == TypedValue.TYPE_INT_BOOLEAN) {
return data != 0 ? "true" : "false";
}
if (type == TypedValue.TYPE_DIMENSION) {
return Float.toString(complexToFloat(data))
+ DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type == TypedValue.TYPE_FRACTION) {
return Float.toString(complexToFloat(data))
+ FRACTION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type >= TypedValue.TYPE_FIRST_COLOR_INT
&& type <= TypedValue.TYPE_LAST_COLOR_INT) {
return String.format("#% 08X", data);
}
if (type >= TypedValue.TYPE_FIRST_INT
&& type <= TypedValue.TYPE_LAST_INT) {
return String.valueOf(data);
}
return String.format("<0x% X, type 0x% 02X>", data, type);
}
private static String getPackage(int id) {
if (id>>>24==1) {
return "android:";
}
return "";
}
public static float complexToFloat(int complex) {
return (float)(complex & 0xFFFFFF00)*RADIX_MULTS[(complex>>4) & 3];
}
private static final float RADIX_MULTS[]={
0.00390625F,3.051758E-005F,1.192093E-007F,4.656613E-010F
};
private static final String DIMENSION_UNITS[]={
"px","dip","sp","pt","in","mm","",""
};
private static final String FRACTION_UNITS[]={
"%","%p","","","","","",""
};
}
实体类
public class AppVersionBean {
//版本号
private String versionCode;
//版本号描述
private String versionName;
//版本号地址
private String apkUrl;
//添加事件
private String addTime;
//apk类型 我当时分患者和医生 所以加了类型
private String type;
private String versionDescription;
public String getVersionDescription() {
return versionDescription;
}
public void setVersionDescription(String versionDescription) {
this.versionDescription = versionDescription;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAddTime() {
return addTime;
}
public void setAddTime(String addTime) {
this.addTime = addTime;
}
public String getVersionCode() {
return versionCode;
}
public void setVersionCode(String versionCode) {
this.versionCode = versionCode;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public String getApkUrl() {
return apkUrl;
}
public void setApkUrl(String apkUrl) {
this.apkUrl = apkUrl;
}
}
控制层代码
@RequestMapping(value = "addVersion",method = RequestMethod.POST)
public ResultJson addVersion(AppVersionBean appVersionBean){
ResultJson resultJson = new ResultJson();
if(CommTools.strIsEmpty(appVersionBean.getType(),appVersionBean.getApkUrl())){
resultJson.setCode("1");
resultJson.setTimestamp(CurrentTimeStamp.SelCurrentTimeStamp());
resultJson.setDescription("参数不能为空");
}
if("1".equals(appVersionBean.getType())){
AppVersionBean apkInfo = AnalysisApk.getApkInfo(FileUtil.uploadPath+"hyxyys.apk");
// AppVersionBean apkInfo = AnalysisApk.getApkInfo("D:\\java\\"+"hyxyys.apk");
appVersionBean.setVersionCode(apkInfo.getVersionCode());
appVersionBean.setVersionName(apkInfo.getVersionName());
}else if("2".equals(appVersionBean.getType())){
AppVersionBean apkInfo = AnalysisApk.getApkInfo(FileUtil.uploadPathPatient+"hygj.apk");
// AppVersionBean apkInfo = AnalysisApk.getApkInfo("D:\\java\\"+"hygj.apk");
appVersionBean.setVersionCode(apkInfo.getVersionCode());
appVersionBean.setVersionName(apkInfo.getVersionName());
}
try {
Integer line = appVersionService.addVersion(appVersionBean);
if(line >0){
resultJson.setCode("0");
resultJson.setTimestamp(CurrentTimeStamp.SelCurrentTimeStamp());
resultJson.setDescription("添加成功");
}else{
resultJson.setCode("1");
resultJson.setTimestamp(CurrentTimeStamp.SelCurrentTimeStamp());
resultJson.setDescription("添加失败");
}
} catch (Exception e) {
e.printStackTrace();
resultJson.setCode("1");
resultJson.setTimestamp(CurrentTimeStamp.SelCurrentTimeStamp());
resultJson.setDescription("添加失败");
}
return resultJson;
}
最后添加一个判断字符串非空的方法
public class CommTools {
/**
* 验证单个字符串是否为空
*
* @author: zhouc52
* @time:2015-12-29 下午07:02:58
*/
public static boolean strIsEmpty(String str)
{
if (str == null) return true;
if ("".equals(str)) return true;
if (str == "") return true;
if (str.length() < 1) return true;
if ("undefined".equals(str)) return true;
return false;
}
/**
* 传入多个参数校验是否为空
*
* @author: zhouc52
* @time:2015-12-30 下午04:08:11
*/
public static boolean strIsEmpty(String... value)
{
for (String str : value)
{
if (strIsEmpty(str)) { return true; }
}
return false;
}
}