• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > Android程序版本更新--通知栏更新下载安装,android程序版本

Android程序版本更新--通知栏更新下载安装,android程序版本

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了android程序源代码,android程序,android 退出程序,android应用程序开发,android驱动usb程序等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Android程序版本更新--通知栏更新下载安装,android程序版本


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"
       >复制代码


复制代码
   /**
     * 连接网络,下载一个文件,并传回进度
     * 
     * @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); }
复制代码

安装完成搞定

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Android程序中--不能改变的事情,android程序--改变
  • Android程序版本更新--通知栏更新下载安装,android程序版本

相关文章

  • 2017-05-26Weex 环境搭建(win7),weex环境搭建win7
  • 2017-05-26Activity 启动模式,activity启动模式
  • 2017-05-26编译器开发系列--Ocelot语言5.表达式的有效性检查,--ocelot有效性
  • 2017-05-26百度地图开发的学习(一),百度地图开发学习
  • 2017-05-26android 图片加载库 Glide 的使用介绍,
  • 2017-05-26Android Bitmap占用内存计算公式,androidbitmap
  • 2017-05-26Android开发学习之路--RxAndroid之初体验
  • 2017-05-26ionic打包项目,运行时报错A problem occurred configuring root project &#39;android&#39;。。。,ionicconfiguring
  • 2017-05-26Android实战简易教程-第七十一枪(异步网络下载网络图片及图片廊制作)
  • 2017-05-26Android中Action Bar的使用

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • android:使用gallery和imageSwitch制作可左右循环滑动的图片浏览器
    • android 关于4.0之后不能直接获取SD卡外部存储路径的问题,androidsd
    • 解决CentOS 7 history命令不显示操作记录的时间和用户身份问题
    • Android系统架构,android系统结构
    • AS400银行核心系统开发中的技术总结--交易和组件写法
    • Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知),android.qq5.0
    • Android--Dialog对话框
    • Android Studio 2.0 beta 7 发布
    • Android开发:控件之WebView
    • [AndroidAnnotations框架]AndroidAnnotations的配置介绍

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有