Android软件版本更新
16lz
2021-01-26
转的:适合新手学习,但在实际项目中不可这么做。 以下是我转的内容: =================================================================== android download dialog file string buffer
各种平台软件更新做法差不多,大体做法如下:
1.服务器存放一个最新版本的xml文件,或者存在数据库,各种做法都可以。
1.1xml文件存储verCode verName updateUrl等信息
1.2用户安装的软件的verCode和服务器上取得的做比较,检测到版本低就更新
2.下面是Android手机软件版本更新的教程
2.1项目目录
2.2编写UpdateManager类
[java] view plain copy print ?
- packagecom.doublejun.update;
- importjava.io.File;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.net.HttpURLConnection;
- importjava.net.MalformedURLException;
- importjava.net.URL;
- importandroid.app.AlertDialog;
- importandroid.app.AlertDialog.Builder;
- importandroid.app.Dialog;
- importandroid.content.Context;
- importandroid.content.DialogInterface;
- importandroid.content.Intent;
- importandroid.content.DialogInterface.OnClickListener;
- importandroid.content.IntentFilter;
- importandroid.net.Uri;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.util.Log;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.widget.ProgressBar;
- publicclassUpdateManager{
- privateContextmContext;
- privatefinalStringupdateMsg="亲,有新版本,快下载吧!";//下载消息提示
- privateDialognoticeDialog;//下载提示对话框
- privateDialogdownloadDialog;//下载进度对话框
- privateProgressBarmProgressBar;//进度条
- privateBooleaninterceptFlag=false;//标记用户是否在下载过程中取消下载
- privateThreaddownloadApkThread=null;//下载线程
- privatefinalStringapkUrl="http://www.ypdm.com/hyjj.apk";//apk的URL地址
- privatefinalStringsavePath="/sdcard/UpdateDemo/";//下载的apk存放的路径
- privatefinalStringsaveFileName=savePath+"hyjjrelease.apk";//下载的apk文件
- privateintprogress=0;//下载进度
- privatefinalintDOWNLOAD_ING=1;//标记正在下载
- privatefinalintDOWNLOAD_OVER=2;//标记下载完成
- privatefinalStringTAG="版本更新";//日志打印标签
- privateHandlermhandler=newHandler(){//更新UI的handler
- @Override
- publicvoidhandleMessage(Messagemsg){
- super.handleMessage(msg);
- switch(msg.what){
- caseDOWNLOAD_ING:
- //更新进度条
- mProgressBar.setProgress(progress);
- break;
- caseDOWNLOAD_OVER:
- downloadDialog.dismiss();
- installApk();
- //安装
- break;
- default:
- break;
- }
- }
- };
- /*
- *构造方法
- */
- publicUpdateManager(Contextcontext){
- this.mContext=context;
- }
- /*
- *检查是否有需要更新,具体比较版本xml
- */
- publicvoidcheckUpdate(){
- //到服务器检查软件是否有新版本
- //如果有则
- showNoticeDialog();
- }
- /*
- *显示版本更新对话框
- */
- privatevoidshowNoticeDialog(){
- AlertDialog.Builderbuilder=newBuilder(mContext);
- builder.setTitle("版本更新");
- builder.setMessage(updateMsg);
- builder.setPositiveButton("更新",newOnClickListener(){
- publicvoidonClick(DialogInterfacedialog,intwhich){
- noticeDialog.dismiss();
- showDownloadDialog();
- }
- });
- builder.setNegativeButton("以后再说",newOnClickListener(){
- publicvoidonClick(DialogInterfacedialog,intwhich){
- noticeDialog.dismiss();
- }
- });
- noticeDialog=builder.create();
- noticeDialog.show();
- }
- /*
- *弹出下载进度对话框
- */
- privatevoidshowDownloadDialog(){
- AlertDialog.Builderbuilder=newBuilder(mContext);
- builder.setTitle("软件更新");
- finalLayoutInflaterinflater=LayoutInflater.from(mContext);
- Viewv=inflater.inflate(R.layout.progress,null);
- mProgressBar=(ProgressBar)v.findViewById(R.id.updateProgress);
- builder.setView(v);
- builder.setNegativeButton("取消",newOnClickListener(){
- publicvoidonClick(DialogInterfacedialog,intwhich){
- downloadDialog.dismiss();
- interceptFlag=true;
- }
- });
- downloadDialog=builder.create();
- downloadDialog.show();
- downloadLatestVersionApk();
- }
- /*
- *下载最新的apk文件
- */
- privatevoiddownloadLatestVersionApk(){
- downloadApkThread=newThread(downloadApkRunnable);
- downloadApkThread.start();
- }
- //匿名内部类,apk文件下载线程
- privateRunnabledownloadApkRunnable=newRunnable(){
- publicvoidrun(){
- try{
- URLurl=newURL(apkUrl);
- HttpURLConnectionconn=(HttpURLConnection)url
- .openConnection();
- conn.connect();
- intlength=conn.getContentLength();
- Log.e(TAG,"总字节数:"+length);
- InputStreamis=conn.getInputStream();
- Filefile=newFile(savePath);
- if(!file.exists()){
- file.mkdir();
- }
- FileapkFile=newFile(saveFileName);
- FileOutputStreamout=newFileOutputStream(apkFile);
- intcount=0;
- intreadnum=0;
- byte[]buffer=newbyte[1024];
- do{
- readnum=is.read(buffer);
- count+=readnum;
- progress=(int)(((float)count/length)*100);
- Log.e(TAG,"下载进度"+progress);
- mhandler.sendEmptyMessage(DOWNLOAD_ING);
- if(readnum<=0){
- //下载结束
- mhandler.sendEmptyMessage(DOWNLOAD_OVER);
- break;
- }
- out.write(buffer,0,readnum);
- }while(!interceptFlag);
- is.close();
- out.close();
- }catch(MalformedURLExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- };
- /*
- *安装下载的apk文件
- */
- privatevoidinstallApk(){
- Filefile=newFile(saveFileName);
- if(!file.exists()){
- return;
- }
- Intentintent=newIntent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.parse("file://"+file.toString()),"application/vnd.android.package-archive");
- mContext.startActivity(intent);
- }
- }
以上代码注释足够详细。不解释了。。
2.3在Main中调用试试吧。
[java] view plain copy print ?
- packagecom.doublejun.update;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- publicclassMainextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- UpdateManagerupdate=newUpdateManager(this);
- update.checkUpdate();
- }
- }
好吧,就这些。希望对你有帮助。
更多相关文章
- 解决Android(安卓)SDK Manager更新、下载速度慢
- Android在layout xml中使用include .
- Android(安卓)RecyclerView更新某条/一条数据
- Android(安卓)productFalvors 多渠道打包方案
- Android使用SimpleAdapter更新ListView里面的Drawable元素
- android上如何写配置文件
- 菜单之二:使用xml文件定义菜单
- Android中sqllite存储海量数据解决办法
- Android点击事件的实现