1. 获取本地的VersionCode 这这里呢 封装成了一个类(可自提)

public class APKVersionCodeUtils {      /**       *获取本地版本号       *      */    public static int getVersionCode(Context mContext) {        int versionCode = 0;        try {            //获取软件版本号,对应AndroidManifest.xml下android:versionCode            versionCode = mContext.getPackageManager().                    getPackageInfo(mContext.getPackageName(), 0).versionCode;        } catch (PackageManager.NameNotFoundException e) {            e.printStackTrace();        }        return versionCode;    }    /**     * 获取版本号名称     *     * @param context 上下文     * @return     */    public static String getVerName(Context context) {        String verName = "";        try {            verName = context.getPackageManager().                    getPackageInfo(context.getPackageName(), 0).versionName;        } catch (PackageManager.NameNotFoundException e) {            e.printStackTrace();        }        return verName;    }}

2.网络请求后台接口做判断 判断你的版本号是否低于接口返回的版本号

这个地方我相信大家都会 就不过多写了

3.在我们的程序需要版本更新的时候 要用Alertdialog提示

Alertdialog提示完以后,如果用户点击下载, 我们要做下载最新Apk的任务

说不多说,上代码

public void loadNewVersionAlertDiaLog() {    AlertDialog.Builder alert = new AlertDialog.Builder(ShowActivity.this);    alert.setTitle("说明");    alert.setMessage("发现新版本,是否立即更新?");    alert.setPositiveButton("更新", new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            downloadApk();        }    });    alert.setNegativeButton("暂不更新", new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {        }    });    alert.create();    alert.show();}
/** * 从服务器端下载最新apk */private void downloadApk() {    //显示下载进度    ProgressDialog dialog = new ProgressDialog(this);    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);    dialog.setCancelable(false);    dialog.show();    new Thread(new DownloadApk(dialog)).start();}
/** * 访问网络下载apk */private class DownloadApk implements Runnable {    private ProgressDialog dialog;    InputStream is;    FileOutputStream fos;    private String TAG;    public DownloadApk(ProgressDialog dialog) {        this.dialog = dialog;    }    @Override    public void run() {        OkHttpClient client = new OkHttpClient();        String url = 从后台接口中获取的接口Apk字段;        Request request = new Request.Builder().get().url(url).build();        try {            Response response = client.newCall(request).execute();            if (response.isSuccessful()) {                Log.d(TAG, "开始下载apk");                //获取内容总长度                long contentLength = response.body().contentLength();                //设置最大值                dialog.setMax((int) contentLength);                //保存到sd卡                File apkFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".apk");                fos = new FileOutputStream(apkFile);                //获得输入流                is = response.body().byteStream();                //定义缓冲区大小                byte[] bys = new byte[1024];                int progress = 0;                int len = -1;                while ((len = is.read(bys)) != -1) {                    try {                        Thread.sleep(1);                        fos.write(bys, 0, len);                        fos.flush();                        progress += len;                        //设置进度                        dialog.setProgress(progress);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                //下载完成,提示用户安装                installApk(apkFile);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            //关闭io流            if (is != null) {                try {                    is.close();                } catch (IOException e) {                    e.printStackTrace();                }                is = null;            }            if (fos != null) {                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }                fos = null;            }        }        dialog.dismiss();    }}

 

到这呢版本更新就完成啦,博主呢也是弄了半天才弄了出来,上面的代码都是可以供砖友们自提的,真的希望能给您带来帮助.

 

 


 

 

 

更多相关文章

  1. 安装android时提示The operation cannot be completed. See the
  2. 修改 ActivityManagerService.java 去掉 android 系统报错和程序
  3. android 对比版本号
  4. Android获取已安装应用信息(图标,名称,版本号,包)
  5. Android进度条、自动提示框、下拉框动态数据加载
  6. Android自动提示--AutoCompleteTextView、MultiAutoCompleteText
  7. Android SDK版本号与API Level及VERSION_CODE的对应关系

随机推荐

  1. Android(安卓)多点触控技术
  2. Android(安卓)Market新增运营商结算方式
  3. Android(安卓)的网络编程(17)-android显
  4. Android(安卓)创建与解析XML(六)—— 比较
  5. Android:保护自己开发的Android应用程序
  6. Android系统架构-----Android的系统体系
  7. [原]如何在Android用FFmpeg+SDL2.0解码显
  8. 『ANDROID』android:layout_gravity和and
  9. android 中管理短信
  10. Android服务器搭建——Jsp+Servlet,返回Js