建立pb
// 创建ProgressDialog对象
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1.获得当前版本的方法
int versionCode = Activity.this.getPackageManager().getPackageInfo("com.bw.myversion", 0).versionCode;
String versionName = Activity.this.getPackageManager().getPackageInfo("com.bw.myversion", 0).versionName;
在有xutils包的时候可以使用
int verCode = VerSionUtils.getVerCode(MainActivity.this);
2. 把获得当前版本号和网络传输的版本号进行对比如果较低,那么弹出框
/**
*
* 弹出对话框通知用户更新程序
*
* 弹出对话框的步骤: 1.创建alertDialog的builder. 2.要给builder设置属性, 对话框的内容,样式,按钮
* 3.通过builder 创建一个对话框 4.对话框show()出来
*/
protected void showUpdataDialog() {
AlertDialog.Builder builer = new AlertDialog.Builder(this);
builer.setTitle("版本升级");
builer.setMessage(updataInfo.getUpdateLog());
// 当点确定按钮时从服务器上下载 新的apk 然后安装
builer.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.i("sss", "下载apk,更新");
// downLoadApk();
downFile(“下载的地址”);
}
});
// 当点取消按钮时进行登录
builer.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builer.create();
dialog.show();
}
3.点击确认后调用开始下载的方法(要new一个ProgressBar)
/**
* 下载文件的方法
* @param url
*/
private void downFile(final String url) {
// 提示框显示
pd.show();
// 开启子线程下载apk
new Thread() {
public void run() {
// 创建HttpClient
HttpClient client = new DefaultHttpClient();
// 请求方式
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
// 请求网络
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
// 设置进度条的最大值
pd.setMax((int) length);
// 获取流
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
// 将流写到sd卡中
if (is != null) {
File file = new File(
Environment.getExternalStorageDirectory(),
"updata.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
// 循环将请求的数据写到sd卡
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
pd.setProgress(count);// 设置当前进度
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
// 告诉HANDER已经下载完成了,可以安装了
down();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
如果有utils包可以使用
public void useUtils(){
HttpUtils http = new HttpUtils();
HttpHandler handler = http.download("http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip",
"/sdcard/httpcomponents-client-4.2.5-src.zip",
true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
new RequestCallBack<File>() {
@Override
public void onStart() {
testTextView.setText("conn...");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
testTextView.setText(current + "/" + total);
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
testTextView.setText("downloaded:" + responseInfo.result.getPath());
}
@Override
public void onFailure(HttpException error, String msg) {
testTextView.setText(msg);
}
});
}
4.下载完成后,关闭pb,跳转安装
/**
* 安装方法
*/
// 提示框消失,进行安装
private void down() {
m_mainHandler.post(new Runnable() {
public void run() {
pd.cancel();
update();
}
});
}
/**
* 安装程序
*/
void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "updata.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
}