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:
<?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>
开始下载:
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%");
正在下载,显示下载进度条:
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);
下载完成,点击可以安装:
//点击安装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学习系列(3)--App自动更新之自定义进度视图和内部存储的更多相关文章
- Android学习系列(2)--App自动更新之通知栏下载
这篇文章是Android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用.1.设计思路,使用VersionCode定义为版本升级参数. android为我们定义版本提供了2个属性:& ...
- Android学习系列(7)--App轮询服务器消息
这篇文章是android开发人员的必备知识. 1.轮询服务器 一般的应用,定时通知消息可以采用轮询的方法从服务器拿取消息,当然实时消息通知的话,建议采用推送服务. 其中需要注意轮询的频率 ...
- Android学习系列(17)--App列表之圆角ListView(续)
http://www.cnblogs.com/qianxudetianxia/archive/2011/09/19/2068760.html 本来这篇文章想并到上篇Android学习系列(16)- ...
- Android学习系列(11)--App列表之拖拽ListView(下)
接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果. 7.重写onTouchEvent()方法. 在这个方法中我们主要是处理 ...
- Android学习系列(37)--App调试内存泄露之Context篇(下)
接着<Android学习系列(36)--App调试内存泄露之Context篇(上)>继续分析. 5. AsyncTask对象 我N年前去盛大面过一次试,当时面试官极力推荐我使用AsyncT ...
- Android学习系列(15)--App列表之游标ListView(索引ListView)
游标ListView,提供索引标签,使用户能够快速定位列表项. 也可以叫索引ListView,有的人称也为Tweaked ListView,可能更形象些吧. 一看图啥都懂了: 1. ...
- Android学习系列(23)--App主界面实现
在上篇文章<Android学习系列(22)--App主界面比较>中我们浅略的分析了几个主界面布局,选了一个最大众化的经典布局.今天我们就这个经典布局,用代码具体的实现它. 1.预览图先看下 ...
- Android学习系列(18)--App工程结构搭建
本文算是一篇漫谈,谈一谈关于Android开发中工程初始化的时候如何在初期我们就能搭建一个好的架构. 关于android架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的 ...
- Android学习系列(10)--App列表之拖拽ListView(上)
研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨. 鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. ...
随机推荐
- [转]【Delphi】 Thread.Queue与Synchronize的区别
前话: 其实大家要学会看源码, 我接下来要说的这些东东,与其等别人讲,还不如自己搞几个代码试一下,印象还深刻点 TThread.Queue和TThread.Synchronize的区别,效果上:二者 ...
- 第2章 排序 | 第10节 计数排序练习题 && 基数排序
对于一个int数组,请编写一个计数排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组. 测试样例: [1,2,3,5,2,3],6 [1,2,2,3,3,5] 计数排序 ...
- ScaleIO与XtremSW Cache如何集成呢?
在ScaleIO上, XtremSW Cache主要有两种部署方式: 把XtremSW Cache在每台server的内部用作cache - 在ScaleIO Data Server(SDS)下做ca ...
- Decorator Wrapper 装饰模式 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- JQuery模仿淘宝天猫魔盒抢购页面倒计时效果
1.效果及功能说明 通过对时间的控制来告诉用户一个活动还剩多少时间,精确到秒.2.实现原理 首先定义活动的截至的时间,要重年份精确到毫秒,在获得当前的年份到秒钟,在用截至时间,减去现在的时间,剩下的还 ...
- C#代码审查工具 StyleCop
SourceAnalysis (StyleCop)的终极目标是让所有人都能写出优雅和一致的代码,因此这些代码具有很高的可读性. SourceAnalysis (StyleCop)不是代码格式化(代码美 ...
- js移除Array中指定元素
首先需要找到元素的下标: var array = [2, 5, 9]; var index = array.indexOf(5); 使用splice函数进行移除: if (index > -1) ...
- Android -- SlidingMenu
实现原理 在一个Activity的布局中需要有两部分,一个是菜单(menu)的布局,一个是内容(content)的布局.两个布局横向排列,菜单布局在左,内容布局在右.初始化的时候将菜单布局向左偏移,以 ...
- PHP基本的语法的小结
一.PHP能做什么? PHP能做什么?我认为它非常强大,仅仅要我能想到的,它都能做,仅仅是我技术能力还不行╮(╯﹏╰)╭.好吧,一张图.基本了解一下吧(ps:PHP的功能不局限于此( ^_^ )) 图 ...
- (链表)反转链表Reverse List
逆转链表是简单而又简单的链表问题,其问题的方法之一可以设置三个指针,一个指向当前结点,一个指向前驱结点,一个指向后继指针 代码如下: class Solution { public: ListNode ...