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
# 检查更新
# 下载
# 安装详解
# android检查更新、下载、安装实现代码
# android实现通知栏下载更新app示例
# Android实现检查并下载APK更新、安装APK及获取网络信息的方法
# Android编程实现应用自动更新、下载、安装的方法
# Android程序版本更新之通知栏更新下载安装
# android中DownloadManager实现版本更新
# 监听下载进度实例
# Android中使用AsyncTask实现文件下载以及进度更新提示
# Android Studio下载更新Android SDK网络异常或无法下载
# Android应用程序更新并下载实例
# Android SDK Manager更新、下载速度慢问题解决办法
# Android中使用AsyncTask实现下载文件动态更新进度条功能
# Android编程实现下载时主界面与详细界面一致更新的方法
# 下载地址
# 最新版本
# 不可用
# 请检查
# 希望能
# 谢谢大家
# 转换为
# 不强
# 新版本
# 对话框
# 判断是否
# 使用了
# 依赖于
# final
# manager
# window
# setCancelable
# int
# return
# boolean
相关文章:
如何高效利用亚马逊云主机搭建企业网站?
南阳网站制作公司推荐,小学电子版试卷去哪里找资源好?
建站之星如何保障用户数据免受黑客入侵?
内网网站制作软件,内网的网站如何发布到外网?
孙琪峥织梦建站教程如何优化数据库安全?
陕西网站制作公司有哪些,陕西凌云电器有限公司官网?
如何快速搭建高效服务器建站系统?
小程序网站制作需要准备什么资料,如何制作小程序?
如何使用Golang安装API文档生成工具_快速生成接口文档
如何通过多用户协作模板快速搭建高效企业网站?
定制建站模板如何实现SEO优化与智能系统配置?18字教程
如何挑选优质建站一级代理提升网站排名?
如何快速搭建个人网站并优化SEO?
建站之星后台管理系统如何操作?
深圳网站制作费用多少钱,读秀,深圳文献港这样的网站很多只提供网上试读,但有些人只要提供试读的文章就能全篇下载,这个是怎么弄的?
深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
浅析上传头像示例及其注意事项
建站中国必看指南:CMS建站系统+手机网站搭建核心技巧解析
合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?
如何在阿里云虚拟服务器快速搭建网站?
网站制作难吗安全吗,做一个网站需要多久时间?
常州企业网站制作公司,全国继续教育网怎么登录?
高防服务器租用首荐平台,企业级优惠套餐快速部署
如何选择适配移动端的WAP自助建站平台?
如何在腾讯云服务器上快速搭建个人网站?
深圳网站制作平台,深圳市做网站好的公司有哪些?
如何在Golang中引入测试模块_Golang测试包导入与使用实践
建站主机功能解析:服务器选择与快速搭建指南
网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?
*服务器网站为何频现安全漏洞?
企业网站制作费用多少,企业网站空间一般需要多大,费用是多少?
如何设置并定期更换建站之星安全管理员密码?
网站图片在线制作软件,怎么在图片上做链接?
TestNG的testng.xml配置文件怎么写
建站主机如何选?高性价比方案全解析
,sp开头的版面叫什么?
如何选择高效稳定的ISP建站解决方案?
Android自定义listview布局实现上拉加载下拉刷新功能
宝塔建站助手安装配置与建站模板使用全流程解析
建站之星收费标准详解:套餐费用及年费价格表一览
广州顶尖建站服务:企业官网建设与SEO优化一体化方案
如何选择CMS系统实现快速建站与SEO优化?
上海网站制作开发公司,上海买房比较好的网站有哪些?
利用JavaScript实现拖拽改变元素大小
建站之星如何快速解决建站难题?
香港服务器网站卡顿?如何解决网络延迟与负载问题?
网站规划与制作是什么,电子商务网站系统规划的内容及步骤是什么?
无锡制作网站公司有哪些,无锡优八网络科技有限公司介绍?
*请认真填写需求信息,我们会在24小时内与您取得联系。