欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 检查更新、下载、安装功能的实现

程序员文章站 2023-12-21 11:35:34
android检查更新、下载、安装 前言: 由于友盟更新即将下线,我们就修改了更新逻辑,自己检查更新、下载、安装,但是检查更新还是要依赖于友盟中的在线参数: 1.ma...

android检查更新、下载、安装

前言:

由于友盟更新即将下线,我们就修改了更新逻辑,自己检查更新、下载、安装,但是检查更新还是要依赖于友盟中的在线参数:

1.mainactivity.java:

public class mainactivity extends baseactivity{
 @override
 public void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  checkupdateutil.checkupdate(this);//检查更新
 }
}

2.checkupdateutil.java:

package com.monkey.monkeymushroom.util;

import android.app.alertdialog;
import android.app.notificationmanager;
import android.content.context;
import android.content.intent;
import android.net.uri;
import android.os.environment;
import android.support.v4.app.notificationcompat;
import android.text.textutils;
import android.view.gravity;
import android.view.view;
import android.view.window;
import android.view.windowmanager;
import android.widget.button;
import android.widget.textview;
import android.widget.toast;

import com.lidroid.xutils.httputils;
import com.lidroid.xutils.exception.httpexception;
import com.lidroid.xutils.http.responseinfo;
import com.lidroid.xutils.http.callback.requestcallback;
import com.umeng.analytics.mobclickagent;

import java.io.file;
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.date;

/**
 * 检查更新工具类
 */
public class checkupdateutil {
 private static notificationcompat.builder builder;
 private static notificationmanager manager;
 private static final int update_id = "0";

 /**
  * 检查更新
  *
  * @param context
  * @return
  */
 public static boolean checkupdate(context context) {
  // 获取友盟在线参数(要更新的版本号)
  string force_version = mobclickagent.getconfigparams(context, "version");
  if (stringutils.isempty(version)) {
   version = "1.0";
  }
  // 版本号转换为数组
  string[] mupdateversionarray = version.split(",");
  string curr_version_name = sysinfoutils.getversionname(context);

  for (int i = 0; i < mupdateversionarray .length; i++) {//循环获取在线参数上设置的版本号
   if (curr_version_name.equals(mupdateversionarray [i])) {//如果有,代表要更新
    if ((mupdateversionarray .length > i + 1) && ("y".equals(mupdateversionarray [i + 1]))) {//判断是否强更
     showupdatedialog(true, context);
    } else {//不强更
     showupdatedialog(false, context);
    }
    return true;// 只要找到对应的版本号,即有更新,结束循环
   }
  }
  return false;//无更新
 }

 /**
  * 显示更新对话框
  *
  * @param isforceupdate 是否强制更新
  */
 public static void showupdatedialog(final boolean isforceupdate, final context context) {
  // 获取更新日志
  string update_log = mobclickagent.getconfigparams(context, "update_log");
  // 最新版本
  string new_version = mobclickagent.getconfigparams(context, "new_version");
  // 获取下载地址
  final string download_path = mobclickagent.getconfigparams(context, "new_version_path");
  if (textutils.isempty(update_log) || textutils.isempty(download_path) || textutils.isempty(new_version)) {
   return;
  }
  logmessage.e("monkey", "更新日志--> " + update_log + " 最新版本--> " + new_version + " 下载地址--> " + download_path);
  //弹框提示
  final alertdialog malertdialog = new alertdialog.builder(context).create();
  malertdialog.show();
  malertdialog.setcancelable(false);
  window window = malertdialog.getwindow();
  window.setgravity(gravity.bottom);
  window.setlayout(windowmanager.layoutparams.match_parent, windowmanager.layoutparams.match_parent);
  view view = view.inflate(context, r.layout.dialog_update, null);
  window.setcontentview(view);

  textview log_head = (textview) view.findviewbyid(r.id.log_head);
  textview msg_tv = (textview) view.findviewbyid(r.id.msg_tv);
  log_head.settext("v" + new_version + "更新日志:");
  msg_tv.settext(update_log);
  button update = (button) view.findviewbyid(r.id.yes_btn);
  button notnow = (button) view.findviewbyid(r.id.no_btn);
  update.setonclicklistener(new view.onclicklistener() {

   @override
   public void onclick(view v) {
    download(context, download_path);
    malertdialog.dismiss();
   }
  });
  notnow.setonclicklistener(new view.onclicklistener() {

   @override
   public void onclick(view v) {
    malertdialog.dismiss();
   }
  });
  if (isforceupdate) {//如果是强制更新,则不显示“以后再说”按钮
   notnow.setvisibility(view.gone);
  }
 }

 /**
  * 下载apk
  */
 private static void download(final context context, string download_path) {
  if (environment.getexternalstoragestate().equals(environment.media_mounted)) {
   string apkname = download_path.substring(download_path.lastindexof("/") + 1);
   string target = environment.getexternalstoragedirectory().getabsolutepath() + "/" + apkname;
   logmessage.e("monkey", "apk target -->" + target);
   if (netutils.isnetconnect(context)) {
    httputils httputils = new httputils(1000 * 10);//为了方便使用了xutils
    httputils.download(download_path, target, false, true, new requestcallback<file>() {
     @override
     public void onstart() {
      super.onstart();
      toastutil.show(context, "正在下载……");
      //创建通知栏下载提示
      builder = new notificationcompat.builder(context);
      builder.setsmallicon(r.drawable.ic_launcher)
        .setongoing(true)
        .setcontenttitle("猴菇先生");
      manager = (notificationmanager) context.getsystemservice(context.notification_service);
     }

     @override
     public void onloading(long total, long current, boolean isuploading) {
      super.onloading(total, current, isuploading);
      logmessage.e("monkey", "--> total " + total + " current " + current);
      int cur = (int) (current * 100 / total);
      logmessage.e("monkey", "cur--> " + cur + "%");
      builder.setprogress(100, cur, false)//更新进度
        .setcontenttext(cur + "%");
      manager.notify(update_id, builder.build());
     }

     @override
     public void onsuccess(responseinfo<file> responseinfo) {
      manager.cancel(update_id);//取消通知栏下载提示
      //下载成功后自动安装apk并打开
      file file = responseinfo.result;
      intent intent = new intent(intent.action_view);
      intent.setflags(intent.flag_activity_new_task);
      intent.setdataandtype(uri.fromfile(file), "application/vnd.android.package-archive");
      context.startactivity(intent);
     }

     @override
     public void onfailure(httpexception e, string s) {
      toastutil.show(context, "当前网络不可用,请检查网络设置");
     }
    });
   } else {
    toastutil.show(context, "当前网络不可用,请检查网络设置");
   }
  } else {
   toastutil.show(context, "sd卡没有插好");
  }
 }
}

3.更新弹框布局文件dialog_update.xml:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <relativelayout
  android:layout_width="310dp"
  android:layout_height="wrap_content"
  android:layout_centerinparent="true"
  android:background="@color/base_white"
  android:paddingbottom="20dp">

  <textview
   android:id="@+id/title"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerhorizontal="true"
   android:layout_marginbottom="20dp"
   android:layout_margintop="20dp"
   android:text="发现新版本"
   android:textsize="20sp" />

  <view
   android:id="@+id/line"
   android:layout_width="match_parent"
   android:layout_height="1dp"
   android:layout_below="@+id/title"
   android:background="#4a7acd" />

  <textview
   android:id="@+id/log_head"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/line"
   android:layout_marginleft="20dp"
   android:layout_margintop="20dp" />

  <textview
   android:id="@+id/msg_tv"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/log_head"
   android:layout_centerhorizontal="true"
   android:layout_marginleft="32dp"
   android:layout_marginright="32dp"
   android:text="更新日志"
   android:textsize="16sp" />

  <linearlayout
   android:layout_width="match_parent"
   android:layout_height="35dp"
   android:layout_below="@+id/msg_tv"
   android:layout_margintop="30dp"
   android:orientation="horizontal">

   <button
    android:id="@+id/yes_btn"
    android:layout_width="0dp"
    android:layout_height="35dp"
    android:layout_marginleft="20dp"
    android:layout_marginright="20dp"
    android:layout_weight="1"
    android:background="#4a7acd"
    android:gravity="center"
    android:text="立即更新"
    android:textcolor="@color/base_white"
    android:textsize="16sp" />

   <button
    android:id="@+id/no_btn"
    android:layout_width="0dp"
    android:layout_height="35dp"
    android:layout_marginright="20dp"
    android:layout_weight="1"
    android:background="#4a7acd"
    android:gravity="center"
    android:text="以后再说"
    android:textcolor="@color/base_white"
    android:textsize="16sp" />
  </linearlayout>
 </relativelayout>
</relativelayout>

更新弹框:

Android  检查更新、下载、安装功能的实现

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: