大佬教程收集整理的这篇文章主要介绍了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); }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://scheR_977_11845@as.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,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程序版本更新之通知栏更新下载安装所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。