public class UpdateTest extends Activity {
public static final String UPDATE_URL = "http://10.1.170.6:8080/android_test/update.apk";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((TextView) findViewById(R.id.tv_main)).setText("版本:" + AppInfo.getVerionName() + "(" + AppInfo.getVerionCode() + ")");
final UpdateInfo info = new UpdateInfo();
info.setToUpdate(true);
info.setUpdateUrl(UPDATE_URL);
info.setVersionCode(2);
info.setUpdateText("1. 更新到新版本 \n 2. 修复若干bug\n 3.没有了,谢谢");
new AlertDialog.Builder(UpdateTest.this).setTitle("更新提示").setMessage(info.getUpdateText()).setCancelable(false)
.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadApk(info);
}
}).setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create().show();
}
private ProgressDialog mProgressDialog;
public void showProgressDialog(CharSequence title, CharSequence message) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
mProgressDialog.setMessage("正在下载");
mProgressDialog.setProgress(0);
mProgressDialog.setTitle(title);
mProgressDialog.setMessage(message);
mProgressDialog.show();
}
private void downloadApk(final UpdateInfo updateInfo) {
new AsyncTask<String, Object, Boolean>() {
@Override
protected void onPreExecute() {
showProgressDialog("", "正在连接服务器...");
}
@Override
protected Boolean doInBackground(String... params) {
try {
Request request = Request.createHttpRequest(updateInfo.getUpdateUrl());
request.setMthod(HttpMethod.Get);
Response response = request.execute();
BufferedInputStream is = response.getBufferedInputStream();
File file = new File(AppInfo.getAppRootPath() + "/pwmob.apk");
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
int length = (int) response.getContentLength(), readLen = 0, currentLen = 0;
publishProgress("正在下载安装程序", length);
byte[] buffer = new byte[10000];
while (currentLen < length) {
readLen = is.read(buffer);
fos.write(buffer, 0, readLen);
currentLen += readLen;
publishProgress(currentLen);
}
buffer = null;
return true;
} catch (IOException e) {
e.printStackTrace(System.err);
} catch (HttpRequestException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onProgressUpdate(Object... values) {
if (values.length > 1) {
mProgressDialog.setTitle(values[0].toString());
mProgressDialog.setMax((Integer) values[1] / 1024);
} else {
mProgressDialog.setProgress((Integer) values[0] / 1024);
}
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 注意本行的FLAG设置
intent.setDataAndType(Uri.fromFile(new File(AppInfo.getAppRootPath() + "/pwmob.apk")),
"application/vnd.android.package-archive");
UpdateTest.this.startActivity(intent);
TestApp.exitApplication();
}
}
}.execute("");
}
Andorid更新
最新推荐文章于 2023-04-04 14:04:01 发布