关于apk下载的一些问题
获取本地的版本号和服务器的版本号进行版本控制
PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
final int version = info.versionCode;//版本号
String versionname = info.versionName;//版本名称
连接服务器apk地址进行下载
ProgressDialog dialog = ProgressDialog.show(MainActivity2.this, "提示", "正在下载中",
false, false);
downloadApk(dialog);
public void downloadApk(final ProgressDialog dialog ){
new Thread() {
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(pref.getString(Constant.Preference.SERVER)+Constant.Url.DOWN);
HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
InputStream is = httpEntity.getContent();
File dir = new File(
Environment.getExternalStorageDirectory()
+ MyConfig.DOWNLOADPATH);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(
Environment.getExternalStorageDirectory()
+ MyConfig.DOWNLOADPATH,
MyConfig.DOWNLOADAPK);
FileOutputStream fileOutputStream = new FileOutputStream(
file);
byte[] buff = new byte[1024];
int ch = -1;
while ((ch = is.read(buff)) != -1) {
fileOutputStream.write(buff, 0, ch);
}
fileOutputStream.flush();
fileOutputStream.close();
is.close();
}
handler.sendEmptyMessage(MyConfig.MSG_APKINSTALL);
dialog.dismiss();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("NetWorkUtils--->downApk---catch exception is "
, e.getMessage());
e.printStackTrace();
// handler.sendEmptyMessage(MyConfig.MSG_DOWNLOADAPK_FAIL);
}
}
}.start();
下载完成后安装apk
Intent intent = new Intent(Intent.ACTION_VIEW);
//MyConfig.DOWNLOADPATH 下载的apk存放文件将夹 "/jdjianxiu/"
//MyConfig.DOWNLOADAPK apk的名称
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory()+ MyConfig.DOWNLOADPATH ,
MyConfig.DOWNLOADAPK)),
"application/vnd.android.package-archive");
startActivity(intent);