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

Android程序版本更新之通知栏更新下载安装,第1张

概述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" ><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"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,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,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程序版本更新之通知栏更新下载安装所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存