在Android应用开发中经常要更新apk
根据当前的应用的versionCode和服务器中的给的versionCode进行比较,判断是否需要更新apk。
xUtils的使用方式:
1.将xUtils的jar包放到项目的libs下,在右键jar包选择Add as Library就可以啦。
public void initData(){
try {
//获取当前应用的版本号
PackageManager manager = this.getPackageManager();
PackageInfo packageInfo = manager.getPackageInfo(getPackageName(), 0);
int versionCode = packageInfo.versionCode;
mTvVersion.setText(versionCode);
//服务器的请求地址
String serviceUrl = "";
URL url = new URL(serviceUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
//设置网络请求方式
connection.setRequestMethod("GET");
if (200 == connection.getResponseCode()){
InputStream is = connection.getInputStream();
String json = StreamUtil.readStream(is);
JSONObject jsonObject = new JSONObject(json);
//获取服务器的versionCode
int serverCode = jsonObject.getInt("versionCode");
//获取更新的apk的描述
mApkDesc = jsonObject.getString("versionDesc");
//获取服务器返回的更新apk的url
mUpDataUrl = jsonObject.getString("versionUrl");
//服务器的版本号大于当前的版本号,提示更新
if (versionCode < serverCode){
//在子线程中不允许弹toast和dialog
//runOnUiThread()实际是调用的handler的post()方法
runOnUiThread(new Runnable() {
@Override
public void run() {
showUpDataDialog();
}
});
}else {
loginMain();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void loginMain() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashActivity.this , MainActivity.class));
}
} , 1000);
}
private void showUpDataDialog() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("更新应用");
dialog.setMessage(mApkDesc);
dialog.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//下载apk
performDownloadApk();
}
});
dialog.setNegativeButton("忽略更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
loginMain();
}
});
// dialog.show(); 实际上也是调用了creat()方法。再show的
//外部点击不能取消,并且返回键也不能
// dialog.setCancelable(false);
AlertDialog alertDialog = dialog.create();
//点击外部对话框不消失,点击返回键或者home键对话框消失
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
/**
* 执行下载apk
*/
private void performDownloadApk() {
//用xUtils进行网络数据下载
HttpUtils http = new HttpUtils();
final File file = new File(Environment.getExternalStorageDirectory() , System.currentTimeMillis()+".apk");
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
http.download(mUpDataUrl, file.getAbsolutePath(), true , new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
dialog.dismiss();
installApk(file);
}
@Override
public void onFailure(HttpException e, String s) {
dialog.dismiss();
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
dialog.setMax((int) total);
dialog.setProgress((int) current);
}
});
}
/**
* 安装apk
*/
private void installApk(File file) {
//调用系统的安装功能
/* <intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content" />
<data android:scheme="file" />
<data android:mimeType="application/vnd.android.package-archive" />
</intent-filter>*/
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri ,"application/vnd.android.package-archive" );
//加上这个标记,让他存在一个单独的栈中
//是为了安装完成后提示是进入应用还是关闭应用
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}