当前位置: 移动技术网 > IT编程>移动开发>Android > Android如何实现APP自动更新

Android如何实现APP自动更新

2019年07月24日  | 移动技术网IT编程  | 我要评论

玉浦团电影,开不了口简谱,美走光

先来看看要实现的效果图:

对于安卓用户来说,手机应用市场说满天飞可是一点都不夸张,比如小米,魅族,百度,360,机锋,应用宝等等,当我们想上线一款新版本app时,先不说渠道打包的麻烦,单纯指上传app到各大应用市场的工作量就已经很大了,好不容易我们把app都上传完了,突然发现一个会导致应用闪退的小bug,这时那个崩溃啊,明明不是很大的改动,难道我们还要再去重新去把各大应用市场的版本再上传更新一次?相信我,运营人员肯定会弄死你的!!

有问题,自然就会有解决问题的方案,因此我们就会想到如果在app里内嵌自动更新的功能,那么我们将可以省去很多麻烦,当然关于这方面功能的第三方sdk有很多。

好了,言归正传,今天我们自己来实现下关于app自动更新。

流程其实并不复杂:当用户打开app的时候,我们让app去发送一个检查版本的网络请求,或者利用服务端向app推送一个透传消息来检查app的版本,如果当前app版本比服务器上的旧,那么我们就提醒用户进行下载更新app,当然在特定的情况下,我们也可以强制的让用户去升级,当然这是很不友好的,尽可能的减少这样的做法。

好了,来梳理下流程,首先既然是一个app的更新,那么我们就需要去下载新的app,然后我们需要一个通知来告诉用户当前的下载进度,再来当app安装包下载完成后,我们需要去系统的安装程序来对app进行安装更新。

知识点:

下载:异步http请求文件下载,并监听当前下载进度(这里我采用了okhttp)

通知:notification(具体用法请自行翻阅api文档)

安装:intent (具体用法请自行翻阅api文档)

来看下具体实现代码:

我们需要一个后台服务来支撑app的下载

import android.app.notification;
import android.app.notificationmanager;
import android.app.pendingintent;
import android.app.service;
import android.content.intent;
import android.graphics.bitmapfactory;
import android.net.uri;
import android.os.ibinder;
import android.support.annotation.nullable;
import android.support.v7.app.notificationcompat;

import com.fangku.commonlibrary.utils.storageutil;
import com.zhy.http.okhttp.okhttputils;
import com.zhy.http.okhttp.callback.filecallback;

import java.io.file;

import okhttp3.call;

/**
 * 自动下载更新apk服务
 * create by: chenwei.li
 * date: 2016-08-14
 * time: 09:50
 * email: lichenwei.me@foxmail.com
 */
public class downloadservice extends service {

 private string mdownloadurl;//apk的下载路径
 private notificationmanager mnotificationmanager;
 private notification mnotification;


 @override
 public void oncreate() {
 super.oncreate();
 mnotificationmanager = (notificationmanager) getsystemservice(service.notification_service);

 }

 @override
 public int onstartcommand(intent intent, int flags, int startid) {
 if (intent == null) {
  notifymsg("温馨提醒", "文件下载失败", 0);
  stopself();
 }
 mdownloadurl = intent.getstringextra("apkurl");//获取下载apk的链接
 downloadfile(mdownloadurl);//下载apk
 return super.onstartcommand(intent, flags, startid);
 }

 @nullable
 @override
 public ibinder onbind(intent intent) {
 return null;
 }

 private void notifymsg(string title, string content, int progress) {

 notificationcompat.builder builder = new notificationcompat.builder(this);//为了向下兼容,这里采用了v7包下的notificationcompat来构造
 builder.setsmallicon(r.mipmap.icon_login_logo).setlargeicon(bitmapfactory.decoderesource(getresources(), r.mipmap.icon_login_logo)).setcontenttitle(title);
 if (progress > 0 && progress < 100) {
  //下载进行中
  builder.setprogress(100, progress, false);
 } else {
  builder.setprogress(0, 0, false);
 }
 builder.setautocancel(true);
 builder.setwhen(system.currenttimemillis());
 builder.setcontenttext(content);
 if (progress >= 100) {
  //下载完成
  builder.setcontentintent(getinstallintent());
 }
 mnotification = builder.build();
 mnotificationmanager.notify(0, mnotification);


 }

 /**
 * 安装apk文件
 *
 * @return
 */
 private pendingintent getinstallintent() {
 file file = new file(storageutil.download_dir + "app文件名");
 intent intent = new intent(intent.action_view);
 intent.setflags(intent.flag_activity_new_task);
 intent.setdataandtype(uri.parse("file://" + file.getabsolutepath()), "application/vnd.android.package-archive");
 pendingintent pendingintent = pendingintent.getactivity(this, 0, intent, pendingintent.flag_update_current);
 return pendingintent;
 }


 /**
 * 下载apk文件
 *
 * @param url
 */
 private void downloadfile(string url) {

 okhttputils.get().url(url).build().execute(new filecallback(storageutil.download_dir, "app文件名") {
  @override
  public void onerror(call call, exception e, int id) {
  notifymsg("温馨提醒", "文件下载失败", 0);
  stopself();
  }

  @override
  public void onresponse(file response, int id) {
  //当文件下载完成后回调
  notifymsg("温馨提醒", "文件下载已完成", 100);
  stopself();


  }

  @override
  public void inprogress(float progress, long total, int id) {
  //progress*100为当前文件下载进度,total为文件大小
  if ((int) (progress * 100) % 10 == 0) {
   //避免频繁刷新view,这里设置每下载10%提醒更新一次进度
   notifymsg("温馨提醒", "文件正在下载..", (int) (progress * 100));
  }
  }
 });


 }
}

然后我们只需要在我们想要的更新app的时候去调起这个服务即可,比如在系统设置里的"版本检查"等

intent intent = new intent(mcontext, downloadservice.class);
intent.putextra("apkurl", "apk下载地址");
startservice(intent);

总结

这里我只是粗略演示本地自动更新app的功能,在实际应用中,我们应该配合服务端来做,比如在用户启动app的时候去比对版本号,如果版本号低于服务器的版本号,那么此时服务端应该给客户端一个透传推送,这里的推送内容应该为新版本app的下载地址,此时就可以根据该地址来下载新版app了,当遇到重大更新,不再对老版本进行兼容的时候,可以强制用户升级,这里的方案有很多,比如调用系统级对话框,让用户没办法取消等操作,这里就不做更多描述。以上就是这篇文章的全部内容,希望对有需要的人能有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网