替换友盟更新的小demo

2024-05-23 11:08
文章标签 更新 替换 demo 友盟

本文主要是介绍替换友盟更新的小demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

友盟更新将在2016-10-15停止更新

友盟更新将在2016-10-15停止更新,之前一直使用友盟更新的小伙伴们是不是正在忙着相应对策,
友盟更新替换友盟提供了两种更新替换方式,一个是使用推送将自己的新版本的下载链接推送到客户端,然后客户通过点击通知栏进行更新,这只是最简洁的处理方式,但是略显low,接下来就是第二种方法,
在闪屏界面请求服务器查询是否有新版本
大致思路
1.在闪屏界面,请求服务器
2.服务器返回服务器上面最新的版本的相关的信息,包含版本信息,描述信息,是否是强制更新……
3.获取本地的已经安装的软件的版本号,与服务器返回的版本号进行比对
4.如果本地的版本号小于服务器,则说明需要更新
5.弹出对话框提示有新版本,询问用户是否更新
6.用户点击确定更新,开启更新服务,下载新版的APK
7.下载完成,安装APK,完成版本更新
这是开启的下载的服务的相关代码
package com.cheletong.gyz.service;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;import com.cheletong.R;
import com.cheletong.activity.start.StartActivity;public class UpdateService extends Service {//标题  private int titleId = 0;private final static int DOWNLOAD_COMPLETE = 0;private final static int DOWNLOAD_FAIL = 1;//文件存储  private File updateDir = null;private File updateFile = null;//通知栏  private NotificationManager updateNotificationManager = null;private Notification updateNotification = null;//通知栏跳转Intent  private Intent updateIntent = null;private PendingIntent updatePendingIntent = null;private Notification.Builder builder;private String title = "你的项目名称";private String downLoadUrl;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stub  return null;}@Overridepublic void onCreate() {super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {//获取传值  titleId = intent.getIntExtra("titleId", 0);downLoadUrl = intent.getStringExtra("url");// Toast.makeText(this,"下载地址:" + downLoadUrl,Toast.LENGTH_SHORT).show();// 如果获取到了一个空的url 就将服务器更新写死的URL设置给他if(TextUtils.isEmpty(downLoadUrl)){// TODO  如果获取不到下载的url 就将存放apk更新的url直接设置进来downLoadUrl = "http://42.121.113.46:8098/cheletong/apk/cheletong.apk";}//创建文件if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {updateDir = new File(Environment.getExternalStorageDirectory(), "app/download/");updateFile = new File(updateDir.getPath(), getResources().getString(titleId) + ".apk");}this.updateNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);builder = new Notification.Builder(this);builder.setSmallIcon(R.drawable.icon); //设置图标// builder.setTicker("显示第二个通知");// builder.setContentTitle("通知"); //设置标题// builder.setContentText("点击查看详细内容"); //消息内容// builder.setWhen(System.currentTimeMillis()); //发送时间// builder.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光// builder.setAutoCancel(true);//打开程序后图标消失// Intent intent = new Intent(MainActivity.this, Center.class);// PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);// builder.setContentIntent(pendingIntent);// Notification notification1 = builder.build();// notificationManager.notify(124, notification1); // 通过通知管理器发送通知this.updateNotification = builder.getNotification();//设置下载过程中,点击通知栏,回到主界面  updateIntent = new Intent(this, StartActivity.class);updatePendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0);//设置通知栏显示内容  // updateNotification.icon = R.mipmap.ic_launcher;// updateNotification.tickerText = "开始下载";// updateNotification.setLatestEventInfo(this,"上海地铁","0%",updatePendingIntent);builder.setTicker("开始下载");builder.setContentTitle(title); //设置标题builder.setContentText("0%"); //消息内容// builder.setContentIntent(updatePendingIntent);this.updateNotification = builder.getNotification();//发出通知  updateNotificationManager.notify(0, updateNotification);//开启一个新的线程下载,如果使用Service同步下载,会导致ANR问题,Service本身也会阻塞  new Thread(new updateRunnable()).start();//这个是下载的重点,是下载的过程  return super.onStartCommand(intent, flags, startId);}private Handler updateHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case DOWNLOAD_COMPLETE:updateNotification.flags |= updateNotification.FLAG_AUTO_CANCEL;//点击安装PendingIntent  Uri uri = Uri.fromFile(updateFile);Intent installIntent = new Intent(Intent.ACTION_VIEW);installIntent.setDataAndType(uri, "application/vnd.android.package-archive");updatePendingIntent = PendingIntent.getActivity(UpdateService.this, 0, installIntent, 0);updateNotification.defaults = Notification.DEFAULT_SOUND;//铃声提醒   // updateNotification.setLatestEventInfo(UpdateService.this, "上海地铁", "下载完成,点击安装。", updatePendingIntent);builder.setTicker("下载完成");builder.setContentTitle(title); //设置标题builder.setContentText("下载完成,点击安装。"); //消息内容builder.setContentIntent(updatePendingIntent);builder.setAutoCancel(true);//打开程序后图标消失updateNotification = builder.getNotification();updateNotificationManager.notify(0, updateNotification);//停止服务  stopService(updateIntent);break;case DOWNLOAD_FAIL://下载失败  // updateNotification.setLatestEventInfo(UpdateService.this, "上海地铁", "下载完成,点击安装。", updatePendingIntent);builder.setTicker("下载失败");builder.setContentTitle(title); //设置标题builder.setContentText("下载失败..."); //消息内容// builder.setContentIntent(updatePendingIntent);builder.setAutoCancel(true);//打开程序后图标消失updateNotification = builder.getNotification();updateNotificationManager.notify(0, updateNotification);break;default:stopService(updateIntent);}}};class updateRunnable implements Runnable {Message message = updateHandler.obtainMessage();public void run() {message.what = DOWNLOAD_COMPLETE;try {//增加权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">;  if (!updateDir.exists()) {updateDir.mkdirs();}if (!updateFile.exists()) {updateFile.createNewFile();}//下载函数,以QQ为例子  //增加权限<uses-permission android:name="android.permission.INTERNET">;  long downloadSize = downloadUpdateFile("http://softfile.3g.qq.com:8080/msoft/179/1105/10753/MobileQQ1.0(Android)_Build0198.apk", updateFile);// TODO 设置下载的连接为从之前页面解析万json之后获取到的url// long downloadSize = downloadUpdateFile(downLoadUrl, updateFile);if (downloadSize > 0) {//下载成功  updateHandler.sendMessage(message);// 下载完成之后自动安装应用Intent intent = new Intent();intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setAction(android.content.Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(updateFile),"application/vnd.android.package-archive");startActivity(intent);}} catch (Exception ex) {ex.printStackTrace();message.what = DOWNLOAD_FAIL;//下载失败  updateHandler.sendMessage(message);}}}@SuppressLint("NewApi")public long downloadUpdateFile(String downloadUrl, File saveFile) throws Exception {//这样的下载代码很多,我就不做过多的说明  int downloadCount = 0;int currentSize = 0;long totalSize = 0;int updateTotalSize = 0;HttpURLConnection httpConnection = null;InputStream is = null;FileOutputStream fos = null;try {URL url = new URL(downloadUrl);httpConnection = (HttpURLConnection) url.openConnection();httpConnection.setRequestProperty("User-Agent", "PacificHttpClient");if (currentSize > 0) {httpConnection.setRequestProperty("RANGE", "bytes=" + currentSize + "-");}httpConnection.setConnectTimeout(10000);httpConnection.setReadTimeout(20000);updateTotalSize = httpConnection.getContentLength();if (httpConnection.getResponseCode() == 404) {throw new Exception("fail!");}is = httpConnection.getInputStream();fos = new FileOutputStream(saveFile, false);byte buffer[] = new byte[4096];int readsize = 0;while ((readsize = is.read(buffer)) > 0) {fos.write(buffer, 0, readsize);totalSize += readsize;//为了防止频繁的通知导致应用吃紧,百分比增加10才通知一次  if ((downloadCount == 0) || (int) (totalSize * 100 / updateTotalSize) - 10 > downloadCount) {downloadCount += 10;// updateNotification.setLatestEventInfo(UpdateService.this, "正在下载", (int) totalSize * 100 / updateTotalSize + "%", updatePendingIntent);builder.setTicker("正在下载");builder.setContentTitle(title); //设置标题builder.setProgress(100, (int) totalSize * 100 / updateTotalSize + 9, false);builder.setContentText("当前下载进度:"+(int) totalSize * 100 / updateTotalSize + "%"); //消息内容// builder.setContentIntent(updatePendingIntent);updateNotification = builder.getNotification();updateNotificationManager.notify(0, updateNotification);}}} finally {if (httpConnection != null) {httpConnection.disconnect();}if (is != null) {is.close();}if (fos != null) {fos.close();}}return totalSize;}}  
这是弹出是否更新的对话框的相关代码

……比较乱,直接上传demo吧
下载地址:http://download.csdn.net/detail/u010838785/9640176

这篇关于替换友盟更新的小demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996307

相关文章

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

C#高效实现Word文档内容查找与替换的6种方法

《C#高效实现Word文档内容查找与替换的6种方法》在日常文档处理工作中,尤其是面对大型Word文档时,手动查找、替换文本往往既耗时又容易出错,本文整理了C#查找与替换Word内容的6种方法,大家可以... 目录环境准备方法一:查找文本并替换为新文本方法二:使用正则表达式查找并替换文本方法三:将文本替换为图

Python批量替换多个Word文档的多个关键字的方法

《Python批量替换多个Word文档的多个关键字的方法》有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉,所以本文给大家介绍了Py... 目录工具准备先梳理一下思路神奇代码来啦!代码详解激动人心的测试结语嘿,各位小伙伴们,大家好!有没有想

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

linux安装、更新、卸载anaconda实践

《linux安装、更新、卸载anaconda实践》Anaconda是基于conda的科学计算环境,集成1400+包及依赖,安装需下载脚本、接受协议、设置路径、配置环境变量,更新与卸载通过conda命令... 目录随意找一个目录下载安装脚本检查许可证协议,ENTER就可以安装完毕之后激活anaconda安装更

Nginx进行平滑升级的实战指南(不中断服务版本更新)

《Nginx进行平滑升级的实战指南(不中断服务版本更新)》Nginx的平滑升级(也称为热升级)是一种在不停止服务的情况下更新Nginx版本或添加模块的方法,这种升级方式确保了服务的高可用性,避免了因升... 目录一.下载并编译新版Nginx1.下载解压2.编译二.替换可执行文件,并平滑升级1.替换可执行文件

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种