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

 更新时间:2016年03月16日 09:37:00   投稿:mrr  
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图片轮播效果非常漂亮,在程序开发中也经常用到,本文给大家分享android实现图片轮播效果的几种方法,对android实现图片轮播相关知识感兴趣的朋友一起学习吧
    2015-12-12
  • Android三种菜单实例分析

    Android三种菜单实例分析

    这篇文章主要介绍了Android三种菜单,较为详细的分析了Android菜单分类及相关使用技巧,需要的朋友可以参考下
    2015-05-05
  • Android 调用发送短信的方法

    Android 调用发送短信的方法

    这篇文章主要介绍了Android 调用发送短信的方法的相关资料,主要实现Android 调用短信的使用,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • 浅谈onTouch先执行,还是onClick执行(详解)

    浅谈onTouch先执行,还是onClick执行(详解)

    onTouch先执行,还是onClick执行?下面小编就为大家带来一篇浅谈onTouch先执行,还是onClick执行(详解)。希望对大家有所帮助。一起跟随小编过来看看吧
    2017-03-03
  • Android自定义View实现游戏摇杆键盘的方法示例

    Android自定义View实现游戏摇杆键盘的方法示例

    Android进阶过程中有一个绕不开的话题——自定义View。最近在做项目中又遇到了,所以下面这篇文章主要给大家介绍了利用Android自定义View实现游戏摇杆键盘的相关资料,操作方式类似王者荣耀的摇杆操作,文中通过示例代码介绍的非常详细,需要的朋友们下面来一起看看吧。
    2017-07-07
  • 浅谈Android Studio 3.0 工具新特性的使用 Android Profiler 、Device File Explorer

    浅谈Android Studio 3.0 工具新特性的使用 Android Profiler 、Device File

    这篇文章主要介绍了浅谈Android Studio 3.0 工具新特性的使用 Android Profiler 、Device File Explorer的相关资料,需要的朋友可以参考下
    2017-11-11
  • 安卓版微信小程序跳一跳辅助

    安卓版微信小程序跳一跳辅助

    这篇文章主要为大家详细介绍了安卓版微信小程序跳一跳辅助,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Android中屏幕密度和图片大小的关系详解

    Android中屏幕密度和图片大小的关系详解

    这篇文章主要介绍了Android中屏幕密度和图片大小的关系详解的相关资料,需要的朋友可以参考下
    2017-05-05
  • android ListView的右边滚动滑块启用方法 分享

    android ListView的右边滚动滑块启用方法 分享

    android ListView的右边滚动滑块启用方法 分享,需要的朋友可以参考一下
    2013-05-05
  • Android编程开发音乐播放器实例

    Android编程开发音乐播放器实例

    这篇文章主要介绍了Android编程开发音乐播放器,结合实例形式分析了Android音乐播放器开发所涉及的SeekBar,ListView,广播接收者(以代码的形式注册Receiver),系统服务,MediaPlayer等技巧,需要的朋友可以参考下
    2016-01-01

最新评论