友好的视觉感知和稳定的不出错表现,来自于我们追求美感和考虑的全面性,博客园从技术的角度,一直我都很欣赏。
这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用。
这一篇是对上一篇《Android学习系列(2)--App自动更新之通知栏下载》的补充,因此只是以点为要,点到为止。
1.内部存储
出于考虑到用户可能禁掉了SDCard或者电脑暂时插在电脑上且为磁盘连接状态等等,对于这么个情况下,我们应该也要保证我们的程序也是能正常的运行。所以我们要考虑内部存储。
我暂时把内部存储定在/data/data/xxxxxappxxxx/files目录,核心代码如下:
2 |
if(android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())){ |
3 |
updateDir = new File(Environment.getExternalStorageDirectory(),Global.downloadDir); |
6 |
updateDir = getFilesDir(); |
8 |
updateFile = new File(updateDir.getPath(),getResources().getString(titleId)+".apk"); |
2.内部存储的权限
一起都运行的正常,但是当我们帮下下来的apk文件执行运行操作的时候,确提示如下,
"解析包错误"??其实你下载的文件并不一定就是坏的或者错误的,也可能是android系统的权限在作怪。在你执行之前,加上如下核心代码:
1 |
String cmd = "chmod +x " +updateFile.getPath(); |
3 |
Runtime.getRuntime().exec(cmd); |
4 |
} catch (IOException e) { |
3.通知栏显示进度条组件的一个bug。
在通知栏设置进度条的可见性,会无缘无故的崩溃。
3 |
updateNotification.contentView.setViewVisibility(progressViewID, View.GONE); |
4.自定义进度条显示视图。
布局文件updata_nitification.xml:
01 |
<?xml version="1.0" encoding="utf-8"?> |
03 |
android:layout_width="fill_parent" |
04 |
android:layout_height="fill_parent" |
05 |
android:orientation="vertical" |
06 |
android:layout_weight="2" |
07 |
android:paddingLeft="5dip"> |
08 |
<LinearLayout android:layout_width="fill_parent" |
09 |
android:layout_height="fill_parent" |
10 |
android:gravity="left|center_vertical" |
11 |
android:orientation="horizontal" |
12 |
android:layout_weight="1"> |
13 |
<ImageView android:src="@drawable/icon" |
14 |
android:layout_width="24dip" |
15 |
android:layout_height="fill_parent" |
16 |
android:scaleType="fitCenter"/> |
17 |
<TextView android:layout_width="wrap_content" |
18 |
android:layout_height="wrap_content" |
19 |
android:text="@string/app_name" |
20 |
android:textColor="#000000" |
21 |
android:paddingLeft="5dip" |
22 |
android:textSize="16dip"/> |
24 |
<LinearLayout android:layout_width="fill_parent" |
25 |
android:layout_height="fill_parent" |
26 |
android:gravity="left" |
27 |
android:orientation="horizontal" |
28 |
android:layout_weight="1"> |
29 |
<TextView android:id="@+id/update_notification_progresstext" |
30 |
android:layout_width="wrap_content" |
31 |
android:layout_height="wrap_content" |
32 |
android:textColor="#8F8F8F" |
33 |
android:textSize="14dip"/> |
34 |
<LinearLayout android:id="@+id/update_notification_progressblock" |
35 |
android:layout_width="fill_parent" |
36 |
android:layout_height="wrap_content" |
37 |
android:orientation="horizontal"> |
38 |
<ProgressBar android:id="@+id/update_notification_progressbar" |
39 |
android:layout_width="fill_parent" |
40 |
android:layout_height="wrap_content" |
41 |
style="?android:attr/progressBarStyleHorizontal"/> |
开始下载:
1 |
updateNotification.contentIntent = updatePendingIntent; |
2 |
updateNotification.contentView.setProgressBar(com.cnblogs.tianxia.subway.R.id.update_notification_progressbar, 100, 0, false); |
3 |
updateNotification.contentView.setTextViewText(com.cnblogs.tianxia.subway.R.id.update_notification_progresstext, "0%"); |
正在下载,显示下载进度条:
1 |
updateNotification.contentView.setProgressBar(com.cnblogs.tianxia.subway.R.id.update_notification_progressbar, 100, (int)(totalSize*100/updateTotalSize), false); |
2 |
updateNotification.contentView.setTextViewText(com.cnblogs.tianxia.subway.R.id.update_notification_progresstext, (int)(totalSize*100/updateTotalSize)+"%"); |
3 |
updateNotificationManager.notify(0, updateNotification); |
下载完成,点击可以安装:
02 |
Uri uri = Uri.fromFile(updateFile); |
03 |
Intent installIntent = new Intent(Intent.ACTION_VIEW); |
04 |
installIntent.setDataAndType(uri, "application/vnd.android.package-archive"); |
05 |
updatePendingIntent = PendingIntent.getActivity(UpdateService.this, 0, installIntent, 0); |
07 |
updateNotification.defaults = Notification.DEFAULT_SOUND; |
08 |
updateNotification.contentIntent = updatePendingIntent; |
09 |
updateNotification.contentView.setViewVisibility(com.cnblogs.tianxia.subway.R.id.update_notification_progressblock, View.GONE); |
10 |
updateNotification.contentView.setTextViewText(com.cnblogs.tianxia.subway.R.id.update_notification_progresstext, "下载完成,点击安装!"); |
11 |
updateNotificationManager.notify(0, updateNotification); |
效果图如下:
