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"
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版本更新
android 版本更新、android app版本更新、android 版本更新框架、android 版本更新实现、android版本更新代码,以便于您获取更多的相关知识。

时间: 2024-08-31 19:29:34

Android程序版本更新之通知栏更新下载安装_Android的相关文章

Android程序版本更新之通知栏更新下载安装

Android应用检查版本更新后,在通知栏下载,更新下载进度,下载完成自动安装,效果图如下: •检查当前版本号 AndroidManifest文件中的versionCode用来标识版本,在服务器放一个新版本的apk,versioncode大于当前版本,下面代码用来获取versioncode的值 PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); int

android 版本检测 Android程序的版本检测与更新实现介绍_Android

做个网站的安卓客户端,用户安装到自己手机上,如果我出了新版本怎么办呢?要有版本更新功能. 本来版本检测最好可以自动进行.但如果每次开启程序,都要先检测一轮,是一种浪费,毕竟版本更新是小概率的事情.或许可以程序开启的时候,判断一下时间,单日就检测,双日就不检测,或者随机什么的,降低一下检测的频率? 我采取的做法是将检测功能做到了菜单上,用户有需要,就手动打开自己检测一下.反正我们这个是网站客户端,有版本更新,在网站上发个通告就行了. 版本检测与更新有以下几个关键步骤: 1.检测有无新版本 2.下载

使用Android系统提供的DownloadManager来下载文件_Android

在android2.3以后android系统提供了一个系统组件来供其他app调用来下载东西,使用起来非常方便. 例如我们可以拿来下载app的新版本apk,同时在同时注册一个广播接收器来接收下载完成时DownloadManager发出的的广播,然后自动安装程序. SDK在API Level 9中加入了DownloadManager服务,可以将长时间的下载任务交给系统,完全由系统管理. 直接看实例代码: package com.hebaijun.downloadtest; import java.i

Android SDK Manager国内无法更新的解决方案_Android

         现在由于GWF,google基本和咱们说咱见了,就给现在在做Android  或者想学习Android 的朋友带来了诸多的不便,最简单的就是Android SDK Manager 你无法更新了.        现在这里有一个解决方案,如下. 1.启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 2.在『Android SDK Manager -

android 版本检测 Android程序的版本检测与更新实现介绍

做个网站的安卓客户端,用户安装到自己手机上,如果我出了新版本怎么办呢?要有版本更新功能. 本来版本检测最好可以自动进行.但如果每次开启程序,都要先检测一轮,是一种浪费,毕竟版本更新是小概率的事情.或许可以程序开启的时候,判断一下时间,单日就检测,双日就不检测,或者随机什么的,降低一下检测的频率? 我采取的做法是将检测功能做到了菜单上,用户有需要,就手动打开自己检测一下.反正我们这个是网站客户端,有版本更新,在网站上发个通告就行了. 版本检测与更新有以下几个关键步骤: 1.检测有无新版本 2.下载

android 从服务器上获取APK下载安装

简单的为新手做个分享.  网上有些资料,不过都是很零散,或是很乱的,有的人说看不懂. 一直有新手说 做到服务器更新APK时没有思路,这里做个简单的分享,希望有不同思路的可以讨论.  下面做个很简单的读取处理和讲解思路. 代码带有注释: try { URL url = new URL(params[0]); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setConnectT

Windows 8.1系统更新下载安装操作步骤

  首先进入应用商店,之后会看到"更新Windows"的巨大提示,我们可以从这里免费更新到Windows 8.1,直接点击进入. 图示:进入应用商店选择"更新Windows" 接下来应用商店会给出Windows 8.1重要新特性和功能的简介,以及更新的相关说明,包括升级需要下载文件的大小.点击"下载"按钮.系统提示在下载过程中可以继续做别的操作,系统会在下载完成后提示后面的操作. 图示:系统升级说明,点击"下载" Win8.1

Android无需root实现apk的静默安装_Android

Android的静默安装似乎是一个很有趣很诱人的东西,但是,用普通做法,如果手机没有root权限的话,似乎很难实现静默安装,因为Android并不提供显示的Intent调用,一般是通过以下方式安装apk: Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startAct

3月30日蚕豆网不推荐的Android应用:盗版《快手下载》

前言:如雨后春笋般杂乱滋生的Android app遍布于网络中.下载平台中.用户的手机中,目前由于监管不严.开放性过大这些应用的安全性便成了当前Android手机最大问题.通讯录被复制.私人照片丢失.信息被公开等等事件的出现,其罪魁祸首当属那些存在安全隐患.内藏恶意代码的Android手机应用.鉴于此情况, 蚕豆网联合<安全管家>定期为大家审查Android应用的安全性,尽我们最大力量来帮助用户来认清app.选择app,把最安全.最稳定的Android环境奉献给广大用户.应用名称:<快手