Android学习系列(3)--App自动更新之自定义进度视图和内部存储
友好的视觉感知和稳定的不出错表现,来自于我们追求美感和考虑的全面性,博客园从技术的角度,一直我都很欣赏。
这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用。
这一篇是对上一篇《Android学习系列(2)--App自动更新之通知栏下载》的补充,因此只是以点为要,点到为止。
1.内部存储
出于考虑到用户可能禁掉了SDCard或者电脑暂时插在电脑上且为磁盘连接状态等等,对于这么个情况下,我们应该也要保证我们的程序也是能正常的运行。所以我们要考虑内部存储。
我暂时把内部存储定在/data/data/xxxxxappxxxx/files目录,核心代码如下:
1 2 3 4 5 6 7 8 | //创建目录和文件 if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())){ updateDir = new File(Environment.getExternalStorageDirectory(),Global.downloadDir); } else { //files目录 updateDir = getFilesDir(); } updateFile = new File(updateDir.getPath(),getResources().getString(titleId)+ ".apk" ); |
2.内部存储的权限
一起都运行的正常,但是当我们帮下下来的apk文件执行运行操作的时候,确提示如下,
"解析包错误"??其实你下载的文件并不一定就是坏的或者错误的,也可能是android系统的权限在作怪。在你执行之前,加上如下核心代码:
1 2 3 4 5 6 | String cmd = "chmod +x " +updateFile.getPath(); try { Runtime.getRuntime().exec(cmd); } catch (IOException e) { e.printStackTrace(); } |
3.通知栏显示进度条组件的一个bug。
在通知栏设置进度条的可见性,会无缘无故的崩溃。
1 2 3 | //下面一句是没有语法错误的,但是会导致程序出错 //为了解决这个问题,后面我们会再progressView外面包裹一层LinearLayout来控制可见性 updateNotification.contentView.setViewVisibility(progressViewID, View.GONE); |
4.自定义进度条显示视图。
布局文件updata_nitification.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 | <? xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_weight="2" android:paddingLeft="5dip"> < LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="left|center_vertical" android:orientation="horizontal" android:layout_weight="1"> < ImageView android:src="@drawable/icon" android:layout_width="24dip" android:layout_height="fill_parent" android:scaleType="fitCenter"/> < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="#000000" android:paddingLeft="5dip" android:textSize="16dip"/> </ LinearLayout > < LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="left" android:orientation="horizontal" android:layout_weight="1"> < TextView android:id="@+id/update_notification_progresstext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#8F8F8F" android:textSize="14dip"/> < LinearLayout android:id="@+id/update_notification_progressblock" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> < ProgressBar android:id="@+id/update_notification_progressbar" android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"/> </ LinearLayout > </ LinearLayout > </ LinearLayout > |
开始下载:
1 2 3 | updateNotification.contentIntent = updatePendingIntent; updateNotification.contentView.setProgressBar(com.cnblogs.tianxia.subway.R.id.update_notification_progressbar, 100 , 0 , false ); updateNotification.contentView.setTextViewText(com.cnblogs.tianxia.subway.R.id.update_notification_progresstext, "0%" ); |
正在下载,显示下载进度条:
1 2 3 | updateNotification.contentView.setProgressBar(com.cnblogs.tianxia.subway.R.id.update_notification_progressbar, 100 , ( int )(totalSize* 100 /updateTotalSize), false ); updateNotification.contentView.setTextViewText(com.cnblogs.tianxia.subway.R.id.update_notification_progresstext, ( int )(totalSize* 100 /updateTotalSize)+ "%" ); updateNotificationManager.notify( 0 , updateNotification); |
下载完成,点击可以安装:
1 2 3 4 5 6 7 8 9 10 11 | //点击安装PendingIntent Uri uri = Uri.fromFile(updateFile); Intent installIntent = new Intent(Intent.ACTION_VIEW); installIntent.setDataAndType(uri, "application/vnd.android.package-archive" ); updatePendingIntent = PendingIntent.getActivity(UpdateService. this , 0 , installIntent, 0 ); updateNotification.defaults = Notification.DEFAULT_SOUND; //铃声提醒 updateNotification.contentIntent = updatePendingIntent; //安装界面 updateNotification.contentView.setViewVisibility(com.cnblogs.tianxia.subway.R.id.update_notification_progressblock, View.GONE); updateNotification.contentView.setTextViewText(com.cnblogs.tianxia.subway.R.id.update_notification_progresstext, "下载完成,点击安装!" ); updateNotificationManager.notify( 0 , updateNotification); |
效果图如下: 如果你喜欢的话,请推荐一下,谢谢大家的支持!
分类:
Android学习系列
标签:
Android
, Android学习系列
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构