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

时间:2022-03-31 08:45:01

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

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

•检查当前版本号

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

?
1
2
packageinfo packageinfo = context.getpackagemanager().getpackageinfo(context.getpackagename(), 0);
int localversion = packageinfo.versioncode;

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

•下载apk文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 下载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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?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根据下载进度进行更新

•更新通知栏进度条

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 更新通知栏
*/ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 安装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);
}

安装完成搞定