这一篇是对上一篇《Android学习系列--App自动更新之通知栏下载》的补充,因此只是以点为要,点到为止。
1.内部存储
出于考虑到用户可能禁掉了SDCard或者电脑暂时插在电脑上且为磁盘连接状态等等,对于这么个情况下,我们应该也要保证我们的程序也是能正常的运行。所以我们要考虑内部存储。
我暂时把内部存储定在/data/data/xxxxxappxxxx/files目录,核心代码如下:
//创建目录和文件
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系统的权限在作怪。在你执行之前,加上如下核心代码:
String cmd = "chmod +x " +updateFile.getPath();
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
3.通知栏显示进度条组件的一个bug。
在通知栏设置进度条的可见性,会无缘无故的崩溃。
//下面一句是没有语法错误的,但是会导致程序出错
//为了解决这个问题,后面我们会再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 app自动更新功能的实现
使用第三方平台(蒲公英)和DownloadManager实现APP自动更新
只需要将新的 APP 复制到网站的下载地址里, 其他的什么也不用做 使用 RADStudio10.3.3 并在 Android 10 测试通过 祝大家使用愉快!
Delphi XE android 更新 升级 自动 在线更新升级APP 然后自动安装的代码实现
Qt for Android实现自动更新app,安装apk文件,qml自动更新 通过调用修改Java函数来实现对私有数据的读写
本篇文章主要介绍了Android应用APP自动更新功能的代码实现,想要实现这个效果的同学可以了解一下。
qt for android 更新APP, 可以实现在app里更新自己 实现更新。
安卓APP自定义异常处理器,Release状态下意外奔溃提示更加友好(代替系统默认的那个讨厌的对话框),不影响Debug模式下LogCat的打印。 ************************************************************************ ...
Android App自动更新app源码
Android给app自定义铃声
Android8.0 申请更新APP,或者分享文件
Android实现自动更新代码,一行代码实现自动更新,最新封装开源库,简洁高效
网上找了一堆Delphi安卓APP程序自动升级方法都大多不太好用,有的需要配置WEB服务器,有的控制下载和升级管理麻烦,于是综合网上资料,根据项目需要整理了一个代码单元,可以直接调用,测试环境Delphi XE 10.3
Android APP应用的自动升级更新,可以在本地或连接HTTP进行自动检测,提示是否升级,自动安装。
xamarin ,android的APP自动更新程序,检测版本,然后下载安装
一行代码让你实现APP自动更新功能
实现 android7、8 以上版本自动更新app 程序,仅供参考。
android 友盟集成app自动更新模块 ,友盟的这一块做的挺不错的 ,集成的demo,注释已添加可直接放入项目中
使用android 自带的DownloadManager实现文件下载,兼容性强,代码简单,注释详细
This is an Android project allowing to realize a circular ImageView in the simplest way possible. USAGE To make a circular ImageView add CircularImageView in your layout XML and add CircularImageView...