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); }

最新文章

  1. webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口
  2. 短信接口API
  3. 如何在MySql中记录SQL日志记录
  4. 介绍开源的.net通信框架NetworkComms框架 源码分析(九) IPConnection
  5. 2016 年沈阳网络赛---QSC and Master(区间DP)
  6. Eclipse Plug-in Hello world
  7. yourphp 的 ThinkTemplate.class.php与ContentReplaceBehavior.class.php
  8. xml问题报错处理
  9. notePad++ 使用
  10. response下载文件 (转载)
  11. Hbase客户端API基础小结笔记(未完)
  12. GUN485项目的总结
  13. android之location01
  14. WebStorm 6.0下运行pomelo项目
  15. iOS 生成.a文件
  16. hadoop之hdfs学习
  17. 演示基于SDL2.0+FFmpeg的播放器
  18. 讲座:采用Store检查邮件(1)
  19. [ Android 五种数据存储方式之二 ] —— 文件存储数据
  20. 重定向URL

热门文章

  1. SpringMVC @RequestBody问题:Unrecognized field , not marked as ignorable
  2. C#的事件
  3. java的final用法
  4. 【Javascript】IE8兼容 背景图片与a标签的onclick事件
  5. Pyqt 获取windows系统中已安装软件列表
  6. poj 1004:Financial Management(水题,求平均数)
  7. oracle使用dbms_metadata包取得所有对象DDL语句
  8. Python3 基本数据类型注意事项
  9. Android系统介绍与框架(转)
  10. HDU 3727 Jewel 可持久化线段树