1.网上找了一些,不是不好用,就是差点东西...所以自己做了一个可用的,以后备用

首先先看代码吧,直接复制就可以使用

在一个按钮事件中,写入一个方法来检查版本

checkVersion();

private void checkVersion() { BaseApplication.context().showDialog(this, "正在检查版本..."); //获取service_ip //String server_ip = "http://172.19.128.53:8080"; //这里是从配置文件中获取的服务器的地址,你可以删除根据自己的写 String server_ip = GetConfigValue.getConfigProperties("server_ip"); String hostUrl = server_ip "/api/mdd/appVersion/selectVersion"; //如果第一次安装没有配置文件需要进入配置界面配置 if ("".equals(server_ip) || server_ip == null || "noFile".equals(server_ip)) { //这里如果获取不到配置文件中的地址,就走到设置页面去设置 //系统未设置 走到设置端 startActivity(new intent(mContext, CommonSettingActivity.class)); return; } String versionCode = null; try { versionCode = this.getPackageManager(). getPackageInfo(this.getPackageName(), 0).versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } //这里一定要注意,这里获取的是build.gradle中的那个versionnum的值. //请求参数 HashMap<String, String> param = new HashMap<String, String>(); param.put("version", versionCode);//1.这个是本地存储的版本号 param.put("apptype", "0");//0是安卓 //1是苹果 hostUrl = HttpUtil.paramGet(hostUrl, param); Type type = new TypeToken<CommonResult<VersionInfo>>() { }.getType(); // HashMap<String, String> header = new HashMap<String, String>(); // header.put("Authorization", "Bearer " BaseApplication.context().getToken()); //这里根据需要看,后台需不需要验证,需不需要添加token验证. OkHttprequest<CommonResult<VersionInfo>> request1 = new OkHttpRequest<CommonResult<VersionInfo>>("POST", hostUrl, null, null, null, type, new OkHttpRequest.CallBack<CommonResult<VersionInfo>>() { @Override public void callBack(CommonResult<VersionInfo> result) { //返回请求结果 BaseApplication.context().closeDialog();//这个是弹出的蒙版框,这个之前有博文写过 if (result.getCode() != 200) { //1.这里如果系统启动,请求版本检测失败就退出系统 //if (result.getCode() != 200 || result.getData() == null) { Toast.makeText(StartActivity.this, "系统错误,请稍后再试", Toast.LENGTH_LONG).show(); finish(); //这里可以添加 System.exit(0)退出系统 return; //返回就可以了,不继续处理 } final VersionInfo versionInfo = result.getData(); if (versionInfo == null) { //2.如果系统没有返回信息,表示没有新版本 Toast.makeText(StartActivity.this, "当前已是最新版本!", Toast.LENGTH_LONG).show(); //如果没有新版本就可以进入系统了,这里根据自己需要打开自己的系统. return; } //1.除了系统出错,以及是最新版本以外,正常返回以后,弹出确认是否更新到最新版本弹框. final AlertDialog.Builder normalDialog = new AlertDialog.Builder(StartActivity.this); normalDialog.setTitle("提醒"); normalDialog.setMessage("是否更新到最新版本?"); normalDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //2.如果用户点击确认更新到最新版本,就调用更新版本方法 //把返回的新版本信息,传到uploadVersion中去 uploadVersion(versionInfo); } }); normalDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //当用户点击取消去判断该版本是否是强制更新版本 //如果用户点击取消,这里做的是这样,先判断该版本是否需要强制升级 //如果需要强制升级,那么系统退出不让用户使用了,如果非强制升级 //就进入系统让用户继续使用. if(StringUtils.isNotEmpty(String.valueOf(result.getData().getForcea())) && result.getData().getForcea() == 1){ //这里根据返回的信息,看看是否该版本需要强制安装 //强制更新 不允许进入系统 finish(); System.exit(0);//强制更新,选择了取消,可以退出系统. }else{ //不是强制更新 取消 进入原来版本系统 //这里如果用户点击了取消,并且该版本不是强制更新版本,就 //允许用户进入之前版本系统,根据自己需求写 finish(); } } }); // 显示 normalDialog.show(); } }, new OkHttpRequest.ErrorBack() { @Override public void errorBack(String msg) { BaseApplication.context().closeDialog(); Toast.makeText(StartActivity.this, "服务暂时不可用:" msg, Toast.LENGTH_LONG).show(); Toast.makeText(StartActivity.this, "请检查服务配置", Toast.LENGTH_LONG).show(); startActivity(new Intent(mContext, CommonSettingActivity.class)); finish(); //关闭当前程序 //如果服务不可用,有可能是访问后台访问不到,这个时候可以,打开设置页面,去 //配置后台地址.等配置 // } }); request1.start(); }

android怎么更新apk(安卓原生开发apk在线升级)(1)

2.然后再看下一个方法uploadVersion

private void uploadVersion(VersionInfo versionInfo) { //1.首先去下载安装包,安装包的地址在,请求后台,返回的内容里 //BaseApplication.context().showDialog(this, "正在安装,请稍等。。"); BaseApplication.context().showDialog(this, "正在下载安装包.."); //2.这里相当于给安装重新命名 String imgNameOri = "ydy_" versionInfo.getVersion(); //3.可以看到这里把安装包,放到了安装目录的/ydy_store的文件夹中去了 String filesDir = BaseApplication.context().getExternalfilesDir( Environment.DIRECTORY_PICTURES).getAbsolutePath() "/ydy_store"; File pictureDirOri = new File(filesDir); if (!pictureDirOri.exists()) { pictureDirOri.mkdirs(); } //1.这种方式安装以后会把安装包删除掉 try { final File file = File.createTempFile( imgNameOri, /* prefix */ ".apk", /* suffix */ pictureDirOri /* directory */ ); //DeviceFinal.FILE_SERVICE versionInfo.getUrl() //2.这里是获取fastdfs的服务器地址,apk安装包是放到fastdfs上的. String img_ip = GetConfigValue.getConfigProperties("img_ip"); String hostUrl = img_ip "/" versionInfo.getUrl(); //3.获取安装包地址,然后通过okhttp,发送请求获取下载. OkHttpRequest<String> request = new OkHttpRequest<String>("POST", hostUrl, null, null, null, String.class, new OkHttpRequest.CallBack<String>() { //OkHttpRequest<String> request = new OkHttpRequest<String>("POST", DeviceFinal.FILE_SERVICE versionInfo.getUrl(), null, null, null, String.class, new OkHttpRequest.CallBack<String>() { @Override public void callBack(String filePath) { BaseApplication.context().closeDialog(); //1.这里等待安装包下载成功以后,然后开始执行installAPK安装 Toast.makeText(StartActivity.this, "下载成功!开始安装..", Toast.LENGTH_LONG).show(); installAPK(file); } }, new OkHttpRequest.ErrorBack() { @Override public void errorBack(String msg) { Toast.makeText(StartActivity.this, "版本文件下载失败,请稍后再试", Toast.LENGTH_LONG).show(); finish(); System.exit(0);//如果出错以后就退出系统 } }); request.download(file); } catch (IOException e) { e.printStackTrace(); } }

android怎么更新apk(安卓原生开发apk在线升级)(2)

3.下载以后,就开始安装apk文件,执行installAPK方法

private void installAPK(File savedFile) { //1.传入下载的文件 这里要注意,启动的intent这里是一个动作 //打开安装界面的动作,不是启动一个activity Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); //2.这里需要做判断如果sdk的版本大于24的话,那么共享文件就不能用 //file:// 直接获取了需要用到FileProvider 共享文件,这个需要做一些配置后面说 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri uri = FileProvider.getUriForFile(this, "com.baidu.idl.face.demo.fileprovider", savedFile); intent.setDataAndType(uri, "application/vnd.android.package-archive"); } else { //3.如果系统sdk,小于24 就可以直接获取文件就可以了. intent.setDataAndType(Uri.fromFile(savedFile), "application/vnd.android.package-archive"); } //3.启动安装程序 startActivity(intent); BaseApplication.context().closeDialog(); finish(); //结束当前程序安装新版本程序 System.exit(0);//安装以后结束程序,重新启动打开新程序 }

android怎么更新apk(安卓原生开发apk在线升级)(3)

4.这个过程中,需要,首先,添加权限,这个必须要在:app\src\main\AndroidManifest.xml添加

否则不报错,也不显示有问题不好查原因,但就是不能用,不会弹出安装界面

<!-- 安装权限--> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

android怎么更新apk(安卓原生开发apk在线升级)(4)

5.然后代码中用到的FileProvider配置,也有一篇文章是说FileProvider怎么用的,这里再提一下吧

先找到自己的applicationid 在build.gradle中:

android怎么更新apk(安卓原生开发apk在线升级)(5)

android怎么更新apk(安卓原生开发apk在线升级)(6)

然后在app\src\main\AndroidManifest.xml文件中:

<provider android:name="androidx.core.content.FileProvider" android:authorities="com.baidu.idl.face.demo.fileprovider"//这里就是applicationid fileprovider android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/update_file_paths" /> </provider>

android怎么更新apk(安卓原生开发apk在线升级)(7)

添加上配置

然后再创建xml文件夹,然后创建xml文件:app\src\main\res\xml\update_file_paths.xml

<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_path" path="." /> </paths>

android怎么更新apk(安卓原生开发apk在线升级)(8)

这样就可以了.

,