1下载

    public void download(String url)
{
Utils.Loge("download",url);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String appName = getAppName(activity);
request.setDestinationInExternalFilesDir(activity, Environment.DIRECTORY_DOWNLOADS, appName + ".apk");
DownloadManager downManager = (DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE);
long Id = downManager.enqueue(request);
listener(Id,appName + ".apk");
}

2监听下载

private void listener(final long Id,String filePath) {
String mypath = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"Android"+File.separator+"data"+
File.separator+ activity.getPackageName()+File.separator + "files" + File.separator + Environment.DIRECTORY_DOWNLOADS + File.separator + filePath;
//mypath = Environment.DIRECTORY_DOWNLOADS + File.separator + filePath;
//File fileFull = new File(path, filePath);
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
cReceiver = new Complete(); // 这是个广播器在下文中有完整代码
cReceiver.setId(Id);
cReceiver.setContext(activity);
cReceiver.setRunnable(new Runnable() {
@Override
public void run() {
File file= new File(mypath);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
data = FileProvider.getUriForFile(activity, "com.haiwaiisland.adventure.fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
data = Uri.fromFile(file);
}
intent.setDataAndType(data, "application/vnd.android.package-archive");
activity.startActivity(intent);
}
});
activity.registerReceiver(cReceiver, intentFilter);
}
//com.haiwaiisland.adventure.fileprovider 这个要在Manifest中配置

package com.BoomGames.Game;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Complete extends BroadcastReceiver {
private long mId;
private Activity mContext;
private Runnable mRunnable;
public void setId(long id){
mId = id;
}
public void setContext(Activity context){
mContext = context;
}
public void setRunnable(Runnable run){
mRunnable = run;
}
@Override
public void onReceive(Context context, Intent intent) {
long ID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (ID == mId) {
if(mRunnable != null){
mRunnable.run();
}
else{
}
}
}
}

3Manifest 增加配置

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.haiwaiisland.adventure.fileprovider"
android:exported="false"
android:grantUriPermissions="true"> <meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
//androidx.core.content.FileProvider 这个是androidx 以前的版本是 android.support.v4.content.FileProvider
//com.haiwaiisland.adventure.fileprovider 这个是 包名.fileprovider
//xml/file_paths 在android res 目录中加 目录xml 在这个目录中新建 file_paths.xml 文件

4 file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path path="" name="download"/>
</paths>
</resources>

今天又测试了一波 bug一堆哦

首先是http 我哪有https呢,我一个客户端技术只能搞个ftp模拟下载 安卓开启http

在 xml/network_security_config 在android res 目录中加 目录xml 内容如下

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>

Manifest 中配置

<application
android:networkSecurityConfig="@xml/network_security_config">

网络安装引用的权限

  <uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

android 下载安装包更新app的更多相关文章

  1. 关于iOS和Android的安装包更新笔记

    关于iOS和Android的安装包更新问题 1. Android更新apk 1)使用DownloadManager下载 2)使用HttpClient下载 apk的下载不能使用ssl,即不能使用http ...

  2. 关闭安装包更新使用YUM在Linux中(RHEL / CentOS / Fedora)

    YUM (Yellowdog Updater Modified)  是一个开源的命令行工具,以及基于图形的软件包管理工具, 用于基于 RPM (RedHat Package Manager) 的 Li ...

  3. Keil 5(C51 与 MDK-ARM)官网下载安装包 [ 图文教程 ]

    前言 本篇我将介绍 Keil C51 和 MDK-ARM 两大集成开发环境的安装包下载方法,帮助大家安全快速的从官网下载安装包. 博主编写了软件安装教程,可以在安装包下载完成后,跳转观看图文教程进行软 ...

  4. Hadoop学习笔记(一)从官网下载安装包

    Hadoop是一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.要学习Hadoop从下载安装包开始 打开 ...

  5. 配置Windows Server2008+iis+php+mysql所需下载安装包

    最近一个朋友让我帮忙给配置服务器iis+php+mysq 环境,遇到了很多问题,特此就在这里说一下.小弟只是在windwos2003 和windwos XP下配置过iis+php+mysql,去朋友那 ...

  6. Linux(CentOS7)yum安装卸载命令,离线下载安装包

    一.Linux版本 二.yum安装 比如安装vim编辑器,y是自动应答,即默认一路确认,不用中途确认 yum install -y vim 三.yum卸载 比如卸载掉刚刚安装的vim yum eras ...

  7. Yum 下载安装包及对应依赖包

    Yum 下载安装包及对应依赖包: 安装该软件:yum install -y yum-plugin-downloadonly 以下载 openssh-server 为例 yum install -y o ...

  8. CentOS7 只下载安装包不安装

    参考文档 yum安装本地rpm软件方案详解 https://www.jb51.net/article/132305.htm createrepo CentOS-Media.repo 本地软件源 --d ...

  9. CentOS yum安装软件时保留安装包及依赖包或者自动下载安装包及相关依赖包

    CentOS上安装某个软件一般都有很多相关的依赖包,当然,这也与我们安装时software selection步骤中选择的版本有关系,我们服务器在安装CentOS时一般选择Basic Web Serv ...

  10. 如何在Mac上切换python2和python3以及下载安装包 & 在Mac上如何查找系统自带python2.7的路径

    电脑:系统是Mac OS  系统自带python2.7  自己下载安装了python3.6 问题:一开始我想在终端下执行python2的相关代码   例如 python kNN.py (kNN.py这 ...

随机推荐

  1. 《TensorFlow+Keras自然语言处理实战》图书介绍

    #好书推荐##好书奇遇季#<TensorFlow+Keras自然语言处理实战>.当当京东天猫均有发售. https://item.jd.com/12788707.html 文后有本书配套源 ...

  2. vue IE9兼容flex布局 css3(转载)

    原文 https://blog.csdn.net/shihezhengshz/article/details/118860562 写这文章的时候的我,心力憔悴鸭,找了好长时间,呜呜┭┮﹏┭┮ 好了,开 ...

  3. db2入门必看命令清单--日常运维必需

    关注 推荐 嘉兴 视频 财经 科技 热点 国际 更多   搜索 34 消息 发布     5   2   收藏 分享 db2入门必看命令清单--日常运维必需 https://www.toutiao.c ...

  4. Mogdb / opengauss 用户密码错误,用户被锁

    # 问题概述xxx客户新上一套opengauss数据库,在测试中程序里用户的密码配置错误,导致用户被锁# 解决方案1.跟oracle 语法一样alter user xxx account unlock ...

  5. 计数 dp 部分例题(十一~十五部分)

    十一.矩阵的利用(行列を用いたテクニック) 1. 快速幂(二分累乗) (1) 推导转移矩阵(行列の導出) 例题:Placing Squares 题解 (2) BM 优化递推(?)(コンパニオン行列の累 ...

  6. java使用array是copyof创建新长度数组

    Arrays.copyof() int[] copied = Arrays.copyOf(arr, 10); //10 the the length of the new array System.o ...

  7. Android studio学习第一期

    下载工具 Android studio 参考博客https://blog.csdn.net/weixin_45406151/article/details/114531103 汉化完成 并创建了虚拟手 ...

  8. centos-7部署kafka-v2.13.3.0.1集群

    1.部署测试机器规划 ip         kafka 版本   zookeeper 版本 192.168.113.132        v2.13.3.0.1 v3.6.3 192.168.113. ...

  9. 宽字符集(unicode)操作函数 (转)

    字符分类: 宽字符函数 普通C函数 描述 iswalnum() isalnum() 测试字符是否为数字或字母 iswalpha() isalpha() 测试字符是否是字母 iswcntrl() isc ...

  10. 使用react脚手架创建项目报错-You are running `create-react-app` 5.0.0, which is behind the latest release (5.0.1).

    创建项目报错: You are running `create-react-app` 5.0.0, which is behind the latest release (5.0.1). We no ...