Androd的版本更新几科是每个应用都要的,现在重新优化了一下此功能,把代码记一下,方便以后使用。
1.首先需要获取当前版本的版本号.其中package.name为你的应用程序的包名,通过此方法,你就能获取当你当前应用的版本号。
versionCode = getActivity().getPackageManager().getPackageInfo("package.name", 0).versionCode;
2.去请求服务器,去获取服务器的版本号。我这里用的网络框架是Xutils,用HttpClient是一
//AppFinal.URL_VERSION_UPDATE为获取服务器版本的网址
http.send(HttpRequest.HttpMethod.GET, AppFinal.URL_VERSION_UPDATE,
new RequestCallBack() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo responseInfo) {
try {
JSONObject jsonObject = new JSONObject(
responseInfo.result);
JSONObject jsonObject2 = jsonObject
.getJSONObject("data");
String version = jsonObject2
.getString("version");
//d为你下载应用的地址
String d = jsonObject2.getString("download");
download = d.concat("/");
//这里就要去判断当前版本与服务器版本的大小,如果服务器版本大,就弹出是否更新提示框。如果一样大,就Tosat下,当前已经是最新版本。
if (versionCode < Integer.parseInt(version)) {
showAlterDialog();
}else {
Toast.makeText(getActivity(), "当前是最新版本", 0).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
3.showAlterDialog();的内容
public void showAlterDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
String str = "是否要更新";
builder.setTitle("更新提示");
builder.setMessage(str);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//如果确定要更新,就去从服务器上进行下载
downFile(download);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}4.downFile(download)的内容private void downFile(final String url) {
private void downFile(final String url) {
//progressDialog.show();
builder = new NotificationCompat.Builder(
getActivity());
final RemoteViews rv = new RemoteViews(getActivity().getPackageName(),
R.layout.item_notication);
rv.setImageViewResource(R.id.imageview, R.drawable.ic_launcher);
builder.setContentTitle("");
builder.setContentText("");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("正在下载中");
manager = (NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
double length;
try {
response = client.execute(get);
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity entity = response.getEntity();
length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(
Environment.getExternalStorageDirectory(),
"phonecvs.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[409600];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
int num = (int) ((count/length)*100);
String str = String.valueOf(num);
//System.out.println("-----str"+str);
rv.setProgressBar(R.id.progressbar, 100, num, false);
rv.setTextViewText(R.id.textview, str);
builder.setContent(rv);
manager.notify(10,builder.build());
}
}
//manager.cancel(10);
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down(); // 告诉HANDER已经下载完成了,可以安装了
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
5.下载完成之后就要去安装这个应用了
private void down() {
handler.post(new Runnable() {
public void run() {
//这句话的作用是当下载完成之后,进度条自动消失
manager.cancel(10);
//progressDialog.cancel();
update();
}
});
}
6.安装代码为
void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "phonecvs.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
} 至此,整个功能就完成了,