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" ><ImageViewandroid:id="@+id/notify_icon_iv"android:layout_width="25dp"android:layout_height="25dp"android:src="@drawable/ic_launcher" /><TextViewandroid: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" /><ProgressBarandroid: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() {@Overridepublic void run() {super.run();try {URL url = new URL(uri);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setDoInput(true);conn.connect();InputStream in = conn.getInputStream();// 2865412long length = conn.getContentLength();// 每次读取1kbyte[] 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);}

安装完成搞定

转载于:https://www.cnblogs.com/dongweiq/p/3926659.html

Android程序版本更新--通知栏更新下载安装(转)相关推荐

  1. android程序内下载文件,android文件下载代码 android程序 里如何编写下载代码

    android开发文件下载在手机报错 package cc.download; import android.app.Activity;import android.os我比任何人都懂你不会真的陪我到 ...

  2. 解决Android SDK Manager无法更新下载 - 猪悟能 - 博客园 (cnblogs.com)

    新机器安装SDK Manager时一直下载失败,连接dl.google.com失败,连接dl-ssl.google.com失败.多次搜索和尝试后发现一个比较好的解决办法如下: 引自:解决Android ...

  3. python版本升级和系统更新下载安装_Python环境安装与升级

    Python是跨平台的,它可以运行在Windows,Mac,Linux/Unix系统上,在Windows上写的Python程序,在Linux上也是能够运行的.目前,Python有两个大版本,一个是2. ...

  4. android程序到处apk,导出已安装到手机中程序的apk文件

    查看该手机所有安装包的包名, 输入adb shell pm list packages 找到你要导出的包名 获取该安装apk的路径, 输入adb shell pm path com.pfoc.myac ...

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

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

  6. Android 检查版本更新 Server后台下载

    问题来了,平时开发应用也许你会遇到这种场景:应用启动检查服务器版本,若大于当前版本,则要从网络上下载APK文件,并在Activity上展示进度条. 版本更新,无非是下载apk文件,安装apk. //其 ...

  7. Android 检查版本更新服务并下载,BLE蓝牙连接,BLE蓝牙连接1对多及通用工具

    https://github.com/inksnow/InksLibrary 引用方法: 1. aar 应用 apply plugin: 'com.android.application' andro ...

  8. 弹框--更新下载--安装

    1 /** 2 * show出对话框 3 */ 4 private void showUpdataDialog() { 5 AlertDialog.Builder builder = new Buil ...

  9. Linux arm 下载程序,arm-linux—gcc如何下载安装

    ubuntu下交叉编译环境构建(arm-linux-gcc-4.4.3-20100728.tar.gz ) 1.下载arm-linux-gcc-4.4.3-20100728.tar.gz到任意的目录下 ...

最新文章

  1. Android Studio自定义模板代码
  2. 组合模式——透明组合模式,安全组合模式
  3. 【干货】理发师都知道的产品经理最容易犯的几个错误
  4. log4net使用详解 .
  5. django restframwork 教程之authentication权限
  6. 拳王虚拟项目公社:闲鱼卖资源还可以卖吗?闲鱼怎么卖虚拟资源?卖什么资源赚钱?闲鱼卖虚拟资源如何赚到钱?
  7. DI、IOC基础学习笔记
  8. python字符串一(字符串的书写输入输出)
  9. wxPython + PyOpenGL 打造三维数据分析的利器!| CSDN 博文精选
  10. python autoitlibrary_记录RF安装AutoItLibrary库的辛酸过程
  11. 判断数组中的元素是否连续
  12. 初级Java程序员如何向Java架构师进阶?这里有6个建议
  13. 电音中DJ/Producer/MC/EDM/Remix/Mix的名词解释(转)
  14. 字节二面、三面面经及内推
  15. hzhost防asp攻击函数
  16. 有度即时通系统安全技术原理
  17. sql中日期转化为各種字符串格式
  18. php数据group去重,MongoDB_Mongodb聚合函数count、distinct、group如何实现数据聚合操作, 上篇文章给大家介绍了Mong - phpStudy...
  19. vue3.0抢先看(附尤雨溪vue分享ppt)
  20. 龙族幻想东京机器人一次_龙族幻想凌晨四点的东京机器人坐标-机器人刷新点_6137游戏网...

热门文章

  1. Pattern类与Matcher方法的验证
  2. 安全强化linux-SELinux
  3. 转:SQL的内连接与外连接
  4. SMI/慧荣/SM32**主控量产通用教程,PNY U盘量产!
  5. android java11,Android RxJava1 入门教程
  6. python通过ip池爬_python爬虫18 | 就算你被封了也能继续爬,使用IP代理池伪装你的IP地址,让IP飘一会...
  7. 如何开始使用LightZone
  8. docker 安全性_未来的Docker安全性
  9. drupal 迁移_关于如何迁移到Drupal的4个技巧
  10. apache web_Web发明家预测文化将发生变化,Apache推动一半的互联网发展,等等