栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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";

 
 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;//无更新
 }

 
 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);
  }
 }

 
 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() {
     @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 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:




 

  

  

  

  

  

   

更新弹框:

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

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/159994.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号