Android应用检查版本更新后,在通知栏下载,更新下载进度,下载完成自动安装,效果图如下:

  • 检查当前版本号

AndroidManifest文件中的versionCode用来标识版本,在服务器放一个新版本的apk,versioncode大于当前版本,下面代码用来获取versioncode的值

PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
int localVersion = packageInfo.versionCode;

用当前versioncode和服务端比较,如果小于,就进行版本更新

  • 下载apk文件
 /**
* 下载apk
*
* @param apkUri
*/
private void downLoadNewApk(String apkUri, String version) {
manager = (NotificationManager) context
.getSystemService((context.NOTIFICATION_SERVICE));
notify = new Notification();
notify.icon = R.drawable.ic_launcher;
// 通知栏显示所用到的布局文件
notify.contentView = new RemoteViews(context.getPackageName(),
R.layout.view_notify_item);
manager.notify(100, notify);
//建立下载的apk文件
File fileInstall = FileOperate.mkdirSdcardFile("downLoad", APK_NAME
+ version + ".apk");
downLoadSchedule(apkUri, completeHandler, context,
fileInstall);
}
FileOperate是自己写的文件工具类

通知栏显示的布局,view_notify_item.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"
android:layout_marginLeft="10dp"
android:background="#00000000"
android:padding="5dp" > <ImageView
android:id="@+id/notify_icon_iv"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/notify_updata_values_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/notify_icon_iv"
android:gravity="center_vertical"
android:text="0%"
android:textColor="@color/white"
android:textSize="12sp" /> <ProgressBar
android:id="@+id/notify_updata_progress"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/notify_icon_iv"
android:layout_marginTop="4dp"
android:max="100" />
</RelativeLayout>
   /**
* 连接网络,下载一个文件,并传回进度
*
* @param uri
* @param handler
* @param context
* @param file
*/
public static void downLoadSchedule(final String uri,
final Handler handler, Context context, final File file) {
if (!file.exists()) {
handler.sendEmptyMessage(-1);
return;
}
// 每次读取文件的长度
final int perLength = 4096;
new Thread() {
@Override
public void run() {
super.run();
try {
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream in = conn.getInputStream();
// 2865412
long length = conn.getContentLength();
// 每次读取1k
byte[] buffer = new byte[perLength];
int len = -1;
FileOutputStream out = new FileOutputStream(file);
int temp = 0;
while ((len = in.read(buffer)) != -1) {
// 写入文件
out.write(buffer, 0, len);
// 当前进度
int schedule = (int) ((file.length() * 100) / length);
// 通知更新进度(10,7,4整除才通知,没必要每次都更新进度)
if (temp != schedule
&& (schedule % 10 == 0 || schedule % 4 == 0 || schedule % 7 == 0)) {
// 保证同一个数据只发了一次
temp = schedule;
handler.sendEmptyMessage(schedule);
}
}
out.flush();
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}

handler根据下载进度进行更新

  • 更新通知栏进度条
/**
* 更新通知栏
*/
private Handler completeHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 更新通知栏
if (msg.what < 100) {
notify.contentView.setTextViewText(
R.id.notify_updata_values_tv, msg.what + "%");
notify.contentView.setProgressBar(R.id.notify_updata_progress,
100, msg.what, false);
manager.notify(100, notify);
} else {
notify.contentView.setTextViewText(
R.id.notify_updata_values_tv, "下载完成");
notify.contentView.setProgressBar(R.id.notify_updata_progress,
100, msg.what, false);// 清除通知栏
manager.cancel(100);
installApk(fileInstall);
}
};
};

下载完成后调用系统安装。

  • 安装apk
/**
* 安装apk
*
* @param file
*/
private void installApk(File file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
context.startActivity(intent);
}

安装完成搞定

Android程序版本更新--通知栏更新下载安装(转)的更多相关文章

  1. 【Android】Android程序自己主动更新

    App自己主动更新的步骤可分为三步: 检查更新(假设有更新进行第2步,否则返回) 下载新版的APK安装包 安装APK 以下对这三步进行解释.当中会穿插相应代码.App自己主动更新的这三步所有被封装到了 ...

  2. 解决Android SDK Manager无法更新下载

    Android Studio2.2版本有一个bug,当在gradle文件编写代码时,程序会自动同步编译,这时整个程序都处于页面卡顿状态,要等待很久才能缓过来.最近实在忍受不了这种龟速,刚好发现有了新的 ...

  3. android studio 3.1.4下载安装配置(附旧版本下载地址)

    windows下安装android studio.当前时间2018年9月. 最新版本的android studio3.2.0-release出来了,拥有许多新的特性 可能我是一个业余的android开 ...

  4. Android sdk manager不能更新下载缓慢的解决方法

    通常情况下,下载Android SDK需要连接谷歌的服务器进行下载,由于国内水深火热的网络,速度基本为0.好在国内也有一个更新的镜像地址.本文章介绍如何在不FQ的情况下,使用国内镜像地址,更新andr ...

  5. Android Studio设置代理更新下载SDK

    代理主机和端口号按下图设置即可,便可以轻松的下载更新SDK啦~~~

  6. Android项目第一天,下载安装Android Studio和“我的第一个安卓项目”

    一.AS的下载我是在AS官方网站进行下载的最新版本,如图所示 二.AS的安装过程 点击你下载的安装包安装即可,傻瓜式一站到底. 到这一步时选择第二个按钮, 随后出现如下界面 这个窗口是提示我们设置代理 ...

  7. Android中实现轮询下载安装应用的Dialog

    前言 因为有涉及到静默安装,主要是针对已经root的设备,应需求,是在ARM的主板上开发的,所以对于常规手机仅能做到轮询下载,做不到静默安装. 效果图 用的鸿洋大神的百分比布局,各个分辨率都完美适配, ...

  8. IIS7上设置MIME让其支持android和Iphone的更新下载

    Android APP的MIME: 文件扩展名:.apk MIME类型:application/vnd.android.package-archive iPhone APP的MIME: 文件扩展名:. ...

  9. android sdk国内快速更新下载

    Android SDK在线更新镜像服务器 http://www.androiddevtools.cn/ 目前正在使用的是:包含详细的配图流程 http://android-mirror.bugly.q ...

随机推荐

  1. js+css3 动画数字累加

    css: .kk{ width:100px; height:100px; display:inline-block; color:red; text-align:center; position: r ...

  2. Python自动化运维之26、Web框架本质、MVC与MTV

    一.Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:ut ...

  3. 七天学会SALTSTACK自动化运维 (2)

    七天学会SALTSTACK自动化运维 (2) 导读 Grains Pillar 总结 参考链接 导读 上一篇主要介绍了安装和基本的使用方法,但是我认为如果理解了相关概念的话,使用会更加顺手,因为毕竟每 ...

  4. 串行CPU设计

    一.概述 串行CPU工作流程 串行CPU的时序流程如下图所示:取指.译码.执行.回写. 其中,取指.回写是与存储器打交道:而译码与执行则是CPU内部自个儿的操作. 我们究竟想要CPU干什么?     ...

  5. Java中遍历Map的几种方法

      转自: http://blog.csdn.net/wzb56/article/details/7864911 方法分为两类: 一类是基于map的Entry:map.entrySet(); 一类是基 ...

  6. 转:理解 PHP 中的 Streams

    本文转自:开源中国社区 [http://www.oschina.net]本文标题:理解 PHP 中的 Streams 本文地址:http://www.oschina.net/translate/und ...

  7. KeilC51使用详解 (一)

    第一节 系统概述 Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性.可维护性上有明显的优势,因而易学易用.用过 ...

  8. Lua参考手册

    英文原版: http://www.lua.org/manual/5.1/ 中文版下面2个地址都有:一样的 manual.luaer.cn lua在线手册 lua参考手册Lua参考手册的中文翻译(云风翻 ...

  9. oracle存储过程 --1

    一,oracle存储过程语法   1.oracle存储过程结构  CREATE OR REPLACE PROCEDURE oracle存储过程名字 (     参数1 IN NUMBER,     参 ...

  10. selenuim ide回放时出现的问题

    [error] Unexpected Exception: fileName -> chrome://selenium-ide/content/selenium-core/scripts/htm ...