您的位置:首页 > 移动开发 > Android开发

Android 在线更新apk

2015-08-31 16:45 381 查看
1.获取当前包的信息:

1 PackageManager manager = Main.this.getPackageManager();
2 try {
3     PackageInfo info = manager.getPackageInfo(Main.this.getPackageName(), 0);
4     String appVersion = info.versionName; // 版本名
5     currentVersionCode = info.versionCode; // 版本号
6     System.out.println(currentVersionCode + " " + appVersion);
7 } catch (NameNotFoundException e) {
8    // TODO Auto-generated catch blockd
9    e.printStackTrace();
10 }
11 //上面是获取manifest中的版本数据,使用了versionCode
12 //在从服务器获取到最新版本的versionCode,比较
13 showUpdateDialog();


2.直接启动线程进行下载管理并安装:

private void showUpdateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("检测到新版本");
builder.setMessage("是否下载更新?");
builder.setPositiveButton("更新", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub                 showDownloadDialog();
            dialog.dismiss();
}
}).setNegativeButton("稍后更新", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
         dialog.dismiss();
}
});
builder.show();
}


3.弹出下载更新进度框,进行apk下载更新:

private void showDownloadDialog(){
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("软件版本更新");

final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.progress, null);
mProgress = (ProgressBar)v.findViewById(R.id.progress);

builder.setView(v);
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
interceptFlag = true;
}
});
downloadDialog = builder.create();
downloadDialog.show();

downloadApk();
}


4.启动线程进行下载任务:

/**
* 下载apk
*/

private void downloadApk(){
downLoadThread = new Thread(mdownApkRunnable);
downLoadThread.start();
}


5.对于下载更新进度框的控制:interceptFlag

private Runnable mdownApkRunnable = new Runnable() {
@Override
public void run() {
try {
URL url = new URL(apkUrl);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();

File file = new File(savePath);
if(!file.exists()){
file.mkdir();
}
String apkFile = saveFileName;
File ApkFile = new File(apkFile);
FileOutputStream fos = new FileOutputStream(ApkFile);

int count = 0;
byte buf[] = new byte[1024];

do{
int numread = is.read(buf);
count += numread;
progress =(int)(((float)count / length) * 100);
//更新进度
mHandler.sendEmptyMessage(DOWN_UPDATE);
if(numread <= 0){
//下载完成通知安装
mHandler.sendEmptyMessage(DOWN_OVER);
break;
}
fos.write(buf,0,numread);
}while(!interceptFlag);//点击取消就停止下载.

fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

}
};


6.进行提示框进度条更新:

private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWN_UPDATE:
mProgress.setProgress(progress);
break;
case DOWN_OVER:

installApk();
break;
default:
break;
}
};
};


7.下载完成,进行apk安装:

/**
* 安装apk
*/
private void installApk(){
File apkfile = new File(saveFileName);
if (!apkfile.exists()) {
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
mContext.startActivity(i);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: