.版本升级 就是拿到清单文件中android:versionCode="1"个服务器versionServer 进行对比,versionServer >android:versionCode 升级,拿到返回的apk下载链接,安装。
1.拿到本地versionCode
//获取版本号
public static int getVersion(Context context){
int version = 0;
try {
version = context.getPackageManager().getPackageInfo(
"你项目的包名", 0).versionCode; //包名对应AndroidManifest.xm package="……"
} catch (Exception e) {
System.out.println("获取版本号异常!");
}
return version;
}
2.拿到本地传到服务器 有服务器判断 返回下载链接
3.开启子线程进行网络请求
//版本升级
String DEVICE_ID=null;//唯一标识码
int version;
String Strversion=null;//版本号转String
Thread serviceThread;
AlertDialog.Builder adb;
private String Downurl=null;//下载链接
serviceThread= new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
String namespace ="http://tempuri.org/";//targetNamespace="http://tempuri.org/">
String transUrl ="";//接口
String method ="VersionUpgrade";//s:element name="VersionUpgrade">
//后台询问或者从wsdl文档或者服务说明中查看
int envolopeVersion =SoapEnvelope.VER12;//
SoapObject request=new SoapObject(namespace, method);
Log.i("TAG", "版本号version====="+Strversion +"识别码DEVICE_ID====="+DEVICE_ID);
request.addProperty("versionNo", version);
request.addProperty("machineCode", Strversion);//本机唯一识别码 不需要可以不传
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(envolopeVersion);
envelope.setOutputSoapObject(request);
envelope.dotNet=true;
HttpTransportSE httpse=new HttpTransportSE(transUrl);
try {
httpse.call(null, envelope);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object response;
try {
response = (Object) envelope.getResponse();
if (response!=null) {//如果返回不为bull 则可以升级,如果是null 则不需要更新
SoapObject result = (SoapObject) envelope.bodyIn;
Downurl = result.getProperty(0).toString();
Log.i("TAG", "response"+response);
Log.i("TAG", "下载链接Downurl"+Downurl);
handler.sendEmptyMessage(1);
}else {
AlertDialog.Builder builder = new Builder(getActivity());
builder.setTitle("版本信息" ) ;
builder.setMessage("当前已经是最新版本" ) ;
builder.setPositiveButton("确定",null);
builder.show();
Log.i("TAG", "无返回值");
Toast.makeText(getActivity(), "无最新版本", Toast.LENGTH_LONG).show();
}
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
4.得到下载链接后
hander 发消息
handler.sendEmptyMessage(1);
5.开启handle
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
showUpdateDialog();是否更新Dialog
Log.i("TAG", "case 1:============case 1:");
break;
}
}
};
private void showUpdateDialog() {
//弹出对话框提示更新
adb = new AlertDialog.Builder(getActivity());
adb.setTitle("发现新版本");
adb.setCancelable(false);//要么点击确定,要么点击取消。否则不会关闭dialog
adb.setNegativeButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//下载更新的APK
downUpdateAPK();
}
});
adb.setPositiveButton("取消",null);
adb.show();
}
6.得到 Url 下载 就好比从服务器下载图片一样了
6.1判断是否有sd卡
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
savePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/update/";
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
apkFilePath = savePath + apkName;
tmpFilePath = savePath + tmpApk;
Log.i("apkUrl","tmpFilePath"+tmpFilePath );
}
6.2 开启线程 下载
downloadThread = new Thread(mdownApkRunnable);
private Runnable mdownApkRunnable = new Runnable() {
@Override
public void run() {
try {
String apkName = "AccessControllSystem" + ".apk";
String tmpApk = "AccessControllSystem" + ".tmp";
// 判断是否挂载了sd卡
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
savePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/update/";
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
apkFilePath = savePath + apkName;
tmpFilePath = savePath + tmpApk;
Log.i("apkUrl","tmpFilePath"+tmpFilePath );
}
// 没有挂载sd卡,无法下载
if (apkFilePath == null || apkFilePath == "") {
mhandler.sendEmptyMessage(DOWN_NOSDCARD);
return;
}
File ApkFile = new File(apkFilePath);
// 输出临时文件
File tempFile = new File(tmpFilePath);
FileOutputStream fos = new FileOutputStream(tempFile);
URL url = new URL(apkUrl);
// 定义url链接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect(); // 建立链接
int length = conn.getContentLength(); // 得到链接内容的
InputStream is = conn.getInputStream();
DecimalFormat df = new DecimalFormat("0.00");
apkFileSize = df.format((float) length / 1024 / 1024) + "MB";
int count = 0;
byte buf[] = new byte[1024];
// 循环下载
do {
int numread = is.read(buf);
count += numread;
tmpFileSize = df.format((float) count / 1024 / 1024) + "MB";
progress = (int) (((float) count / length) * 100);
Log.i("TAG", "tmpFileSize"+tmpFileSize);
mhandler.sendEmptyMessage(DOWN_UPDATE);
if (numread <= 0) {
if (tempFile.renameTo(ApkFile)) {
// 下载完成
mhandler.sendEmptyMessage(DOWN_OVER);
}
break;
}
fos.write(buf, 0, numread);
} while (!interceptFlag);
fos.close();
is.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
6.4安装
private void installApk() {
File apkfile = new File(apkFilePath);
if (!apkfile.exists()) {
return;
}
// 如果存在,启动安装流程
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()),"application/vnd.android.package-archive");
context.startActivity(i);
}
1.启动网络webservice 通过SOAP(是web service的标准通信协议,SOAP为simple object access protocoll的缩写,简单对象访问协议. 它是一种标准化的传输消息的XML消息格式).需要导jar: ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar
2.开启线程