App版本更新兼容7.0
来源:互联网 发布:finalcut mac 破解版 编辑:程序博客网 时间:2024/06/05 11:02
最近在做App版本更新这块的功能,以前也做过,为节省时间,提高开发效率,第一反应就是把之前的代码找出来直接用。三下五除二就搞定了,直接打个包给到测试!果然,一切都太顺利了,只见测试胸有成竹的走过来,说道:你这个版本更新有问题,没法安装!心里咯噔一下,怎么可能,我自己都测试OK的!立马反应过来,那估计是系统问题吧,后来确认,人家测试直接用的7.0的手机测试的。唉,好吧,那就改呗!
1.实现思路:
思路很简单,先判断是否需要版本更新,需要更新则启动一个服务去下载最新的apk,并注册一个广播监听下载进度,下载完成则安装apk。
2.代码实现:
(1)启动服务,若是6.0以上需要动态申请权限
Intent intent=new Intent(this, DownloadApkService.class); intent.putExtra(DownloadApkService.DOWNLOAD_URL,"http://www.huiqu.co/public/download/apk/huiqu.apk"); startService(intent);(2)在服务中下载
public int onStartCommand(Intent intent, int flags, int startId) { String downLoadUrl = intent.getStringExtra(DOWNLOAD_URL); mReceiver = new ApkInstallReceiver(); registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); if (DownLoadUtils.getInstance(this).canDownload()){ downloadApk(this,downLoadUrl,"app下载", appName); }else { DownLoadUtils.getInstance(this).skipToDownloadManager(); } return Service.START_NOT_STICKY; }(3)下载完安装,这里是重点:
public class ApkInstallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { long downloadApkId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); installApk(context); DownloadApkService.this.stopSelf(); } } /** * 安装apk */ public void installApk(Context context) { File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + DownloadApkService.DOWNLOADPATH + appName); if (!file.exists()) { Log.e(TAG, "file is not exists"); return; } Intent intent = new Intent(Intent.ACTION_VIEW); String type = "application/vnd.android.package-archive"; Uri downloadUri = null; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { downloadUri = Uri.fromFile(file); } else { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); downloadUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(downloadUri, type); context.startActivity(intent); } }3.查看AppUpdateDemo
阅读全文
0 0
- App版本更新兼容7.0
- APP版本更新(兼容6.0,7.0系统)
- app版本兼容
- Android 版本更新工具 兼容7.0
- APP版本兼容,伤不起!
- app开发之版本兼容
- 更新app版本
- app 检查版本更新
- ios app 版本更新
- APP版本更新
- App 版本更新 versionUpdate
- app版本更新问题
- App版本的更新
- app版本更新, 提醒
- app版本更新提醒
- iOS APP版本更新
- google app版本更新
- app版本的更新
- 指针函数与函数指针的区别
- jsp通过URL向页面传递中文参数乱码问题
- antd文档系统解析
- caffe可视化与常见问题分析
- JavaScript-定时器0~9抽奖系统
- App版本更新兼容7.0
- cd 在windows下 无法切换盘符目录
- ThinkPHP框架中使用selected
- Java四种线程池的使用
- 前缀表达式运算
- IntelliJ IDEA 创建Maven 工程
- 栈的基本操作及应用
- python 网络框架twisted基础学习及详细讲解
- 听课笔记(17-8-16)