替换友盟更新的小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

相关文章

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

Python如何判断字符串中是否包含特殊字符并替换

《Python如何判断字符串中是否包含特殊字符并替换》这篇文章主要为大家详细介绍了如何使用Python实现判断字符串中是否包含特殊字符并使用空字符串替换掉,文中的示例代码讲解详细,感兴趣的小伙伴可以了... 目录python判断字符串中是否包含特殊字符方法一:使用正则表达式方法二:手动检查特定字符Pytho

C#继承之里氏替换原则分析

《C#继承之里氏替换原则分析》:本文主要介绍C#继承之里氏替换原则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#里氏替换原则一.概念二.语法表现三.类型检查与转换总结C#里氏替换原则一.概念里氏替换原则是面向对象设计的基本原则之一:核心思想:所有引py

Oracle 通过 ROWID 批量更新表的方法

《Oracle通过ROWID批量更新表的方法》在Oracle数据库中,使用ROWID进行批量更新是一种高效的更新方法,因为它直接定位到物理行位置,避免了通过索引查找的开销,下面给大家介绍Orac... 目录oracle 通过 ROWID 批量更新表ROWID 基本概念性能优化建议性能UoTrFPH优化建议注

Redis中6种缓存更新策略详解

《Redis中6种缓存更新策略详解》Redis作为一款高性能的内存数据库,已经成为缓存层的首选解决方案,然而,使用缓存时最大的挑战在于保证缓存数据与底层数据源的一致性,本文将介绍Redis中6种缓存更... 目录引言策略一:Cache-Aside(旁路缓存)策略工作原理代码示例优缺点分析适用场景策略二:Re

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Pandas利用主表更新子表指定列小技巧

《Pandas利用主表更新子表指定列小技巧》本文主要介绍了Pandas利用主表更新子表指定列小技巧,通过创建主表和子表的DataFrame对象,并使用映射字典进行数据关联和更新,实现了从主表到子表的同... 目录一、前言二、基本案例1. 创建主表数据2. 创建映射字典3. 创建子表数据4. 更新子表的 zb

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.