上个星期,项目准备打包,然后做了一个版本更新。版本更新需要准备两个条件。一个是可以从服务器获得当前版本的接口(如果是文件,自己解析也行),一个是将已打包的项目放在服务器上。(我是直接给后台了)下面就可以更新本地了,下面附源代码。
//0x02是比较版本号 //0x03 是安装apk,并删除apk
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0x02:
sCode = msg.arg1;
PackageManager manager = TestVersionActivity.this
.getPackageManager();
PackageInfo info = null;
try {
info = manager.getPackageInfo(
TestVersionActivity.this.getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
int cCode = info.versionCode;
if (sCode > cCode) {
AlertDialog.Builder builder = new AlertDialog.Builder(
TestVersionActivity.this);
builder.setTitle("!版本更新!");
builder.setMessage("是否立即更新版本");
builder.setPositiveButton("取消", null);
builder.setNegativeButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
download();
}
});
builder.show();
} else {
Toast.makeText(TestVersionActivity.this, "更新检查完毕", 0).show();
}
break;
case 0x03:
File apkFile = new File(Environment.getExternalStorageDirectory(),"gametest.apk");
if (!apkFile.exists())
return;
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://"+ apkFile.toString());
intent.setDataAndType(uri,"application/vnd.android.package-archive");
TestVersionActivity.this.startActivity(intent);
break;
default:
break;
}
};
};
//监听事件 去获取服务器上最高版本号,然后传回handler主线程判断
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
String url = HttpUtils.getString(localeSite);
JSONObject json = null;
try {
json = new JSONObject(url);
String serviceCode = json.getString("versioncode");
int i = Integer.parseInt(serviceCode);
Message msg = handler.obtainMessage(0x02);
msg.arg1 = i;
msg.sendToTarget();
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
}
//下载最新apk 并显示下载进度条,下载完成后通知主线程安装最新apk
public void download(){
final ProgressDialog pd; //进度条对话框
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
pd.show();
new Thread(new Runnable() {
@Override
public void run() {
HttpUtils.download("http://192.168.81.219:8080/gametest.apk",pd);
handler.sendEmptyMessage(0x03);
}
}).start();
}
//下载apk ,并安装的工具类
public static void download(String webSite,ProgressDialog pd) {
InputStream isInputStream = null;
FileOutputStream fos = null;
URL url;
try {
url = new URL(webSite);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(5000);
//设置max
pd.setMax(connection.getContentLength());
if (connection.getResponseCode() == 200) {
isInputStream = connection.getInputStream();
fos = new FileOutputStream(Environment.getExternalStorageDirectory()+File.separator+"gametest.apk");
byte[] buf = new byte[1024];
int n = -1;
int total=0;
while((n=isInputStream.read(buf))!=-1){
fos.write(buf, 0, n);
total+=n;
pd.setProgress(total);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(isInputStream!=null){
try {
isInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
isInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这样,版本更新就完成了。
!!!提示,安装包是需要签名的apk文件的,直接编译的在此安装会显示没有签名。另外安装 需要权限的
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 读写ad卡的权限当然也是必要的