上一节,通过解析XML文件来比对版本号信息,接着开始下载和安装apk。
将要安装的apk文件通过tomcat发布,如下:
<xml version="1.0" encoding="utf-8">
<appversion>10003</appversion>
<appfilename>ProjectProgressManager.apk</appfilename>
<appurl>http://192.168.11.106:8080/versionupdatedir/ProjectProgressManager.apk</appurl>
</xml>
xml文件用来定义版本号,apk文件名和下载链接,通过解析这些信息可以获得下载所需的信息。
上一节在比对版本需要升级后点击确定只是弹出了一个toast,这里将实现点击确定后的下载和自动安装。
public void checkappversion() {
checkversionProgressDialog = CustomProgressDialog
.createDialog(ChooseAliveServer.this);
checkversionProgressDialog.setCancelable(false);
checkversionProgressDialog.setTitile("正在检查版本更新信息");
checkversionProgressDialog.setMessage("正在检查版本更新信息.....");
checkversionProgressDialog.show();
if (isAppNeedUpdate()) {
checkversionProgressDialog.dismiss();
final AlertDialog appversionAlertDialog = new AlertDialog.Builder(
ChooseAliveServer.this).create();
appversionAlertDialog.show();
appversionAlertDialog
.setContentView(R.layout.customer_alertdialog_model01);
appversionAlertDialog.setCancelable(false);
TextView successtitle = (TextView) appversionAlertDialog
.findViewById(R.id.cs_alert_01_title);
successtitle.setText("更新提示");
TextView successcontent = (TextView) appversionAlertDialog
.findViewById(R.id.cs_alert_01_content);
successcontent.setText("当前版本号为" + currentversioncode + ",最新版本号为"
+ updateversioncode + ",点击确定进行更新!");
appversionAlertDialog
.findViewById((R.id.cs_alert_01_yes)).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO
// Auto-generated
// method
// stub
/*
* Toast.makeText(ChooseAliveServer.this, "您选择了更新!",
* Toast.LENGTH_LONG).show();
*/
// 点击确定弹出下载进度条
showdownloadprogress();
appversionAlertDialog.dismiss();
}
});
//编写apk下载进度条:
private void showdownloadprogress() {
AlertDialog.Builder downloadpgBuilder = new Builder(this);
downloadpgBuilder.setTitle("正在下载文件包" + apkFilename + apkDownloadUrl);
final LayoutInflater inflater = LayoutInflater
.from(ChooseAliveServer.this);
View pgView = inflater.inflate(R.layout.softupdate_progress, null);
download_ProgressBar = (ProgressBar) pgView
.findViewById(R.id.update_progress);
downloadpgBuilder.setView(pgView);
// 点击取消,进度条消失同时将下载参数置为true;
downloadpgBuilder.setNegativeButton("取消下载",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
Download_cancle = true;
}
});
downloaDialog = downloadpgBuilder.create();
downloaDialog.setCancelable(false);
downloaDialog.show();
downloadApk(apkDownloadUrl);
}
public void downloadApk(String downloadURL) {
class downloadApkThread extends Thread {
public void run() {
// 判断sd卡是否存在,并且是否具有读写权限
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 获取sd卡的路径
String sdpath = Environment.getExternalStorageDirectory()
+ "/";
apksavepath = sdpath + "download";
URL downloadapkUrl = null;
HttpURLConnection downloadapk_Connection = null;
int apklength = 0;
File filepath = new File(apksavepath);
if (!filepath.exists()) {
filepath.mkdir();
System.out.println("xxxxxxxxxxxxxxx" + apksavepath
+ "create");
}
if (filepath.exists()) {
System.out.println("xxxxxxxxxxxxxxx" + apksavepath
+ " is exist");
}
File apkFile = new File(apksavepath, apkFilename);
if (apkFile.exists()) {
apkFile.delete();
}
try {
downloadapkUrl = new URL(apkDownloadUrl);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
downloadapk_Connection = (HttpURLConnection) downloadapkUrl
.openConnection();
downloadapk_Connection.connect();
apklength = downloadapk_Connection.getContentLength();
apkdownInputStream = downloadapk_Connection
.getInputStream();
apkdownloadFileOutputStream = new FileOutputStream(
apkFile);
} catch (Exception e) {
// TODO: handle exception
}
int apkcount = 0;
byte[] buffer = new byte[1024];
do {
int numread;
try {
numread = apkdownInputStream.read(buffer);
apkcount += numread;
// 计算进度条位置
Download_progress = (int) (((float) apkcount / apklength) * 100);
// 更新进度
downloadHandler.sendEmptyMessage(Downloading);
if (numread <= 0) {
// 下载完成
downloadHandler
.sendEmptyMessage(Download_Finish);
break;
}
// 写入文件
apkdownloadFileOutputStream.write(buffer, 0,
numread);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} while (!Download_cancle && !Download_finished);// 点击取消或下载完成就停止下载.
try {
apkdownloadFileOutputStream.close();
apkdownInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
Toast.makeText(ChooseAliveServer.this, "请确认SD卡已经插上并有读写权限!",
Toast.LENGTH_LONG).show();
}
}
}
new downloadApkThread().start();
}
//安装apk
private void installApk() {
File apkFile = new File(apksavepath, apkFilename);
if (!apkFile.exists()) {
return;
}
Intent install_Intent = new Intent(Intent.ACTION_VIEW);
install_Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install_Intent.setDataAndType(
Uri.parse("file://" + apkFile.toString()),
"application/vnd.android.package-archive");
ChooseAliveServer.this.startActivity(install_Intent);
// ChooseAliveServer为activity的类名
/*File installapkFile = new File(apksavepath, apkFilename);
if (!installapkFile.exists()) {
return;
}
Intent install_Intent = new Intent();
install_Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install_Intent.setAction(Intent.ACTION_VIEW);
String typeString="application/vnd.android.package-archive";
install_Intent.setDataAndType(Uri.fromFile(installapkFile), typeString);
startActivity(install_Intent);*/
}
进度条所需的xml文件 R.layout.softupdate_progress:
<?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="wrap_content">
<ProgressBar
android:id="@+id/update_progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" />
</LinearLayout>
当然不要忘了给相应
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
R.layout.customer_alertdialog_model01如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/alert_linearlayout_bg01"
android:orientation="vertical" >
<TextView
android:id="@+id/cs_alert_01_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="12dip"
android:gravity="center"
android:text="警示标题"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#4400ff00" />
<TextView
android:id="@+id/cs_alert_01_content"
android:layout_width="280sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="12dip"
android:layout_marginTop="12dip"
android:gravity="bottom"
android:padding="8dip"
android:text="警示内容"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#4400ff00" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/cs_alert_01_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="14dip"
android:layout_marginLeft="14dip"
android:layout_marginRight="34dip"
android:layout_marginTop="12dip"
android:background="@drawable/button_style"
android:text="确定"
android:textSize="12sp" />
<Button
android:id="@+id/cs_alert_01_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="14dip"
android:layout_marginLeft="34dip"
android:layout_marginRight="14dip"
android:layout_marginTop="12dip"
android:background="@drawable/button_style"
android:text="取消"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
效果如下: