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. c基础-Makefile

    Makefile gcc 测试c编译流程.c->.i->.s->.o->可执行程序 # a.c->a.i->a.s->a.o->a# .i 文件生成 - ...

  2. 备份Cisco交换机设备配置

    需求: 备份网络核心设备配置 工具: 1.3CDaemon软件,用于配置TFTP服务器 链接:http://www.china-ccie.com/download/3CDaemon/3CDaemon. ...

  3. Vulnhub 靶场 HACK ME PLEASE: 1

    Vulnhub 靶场 HACK ME PLEASE: 1 一.前期准备 靶机下载地址:https://www.vulnhub.com/entry/hack-me-please-1,731/ 攻击机ka ...

  4. Advent of Code 2015: Day 10

    JP's Blog GITHUB * FLICKR * RESUME  Search Programming Reviews Photography Maker Writing Research RS ...

  5. rest-assured:JUnit5中接口测试参数化的支持之利用EnumSource检查用户消息是否包含特定字段(JSON path $ doesn't match.)

    代码:package ghcdgj.restful.framework;import static org.junit.jupiter.api.Assertions.*;import static i ...

  6. darkriscv笔记

    1 按照默认设置,前4K空间为ROM,后4K空间是RAM.为什么RAM需要初值,初值从哪儿来? 2 指令分类 LUI: AUTPC: JAL: JALR: BCC : BEQ/BGE /BGEU/BL ...

  7. angular 用法总结

    架构: 1.一个总页面,然后用组件组成 2.每个组件又由三个文件组成,html,css,ts,三种不同的语言放在三个不同的文件中, 3.ts是总配置文件,里面联合各个其他文件(html,css) 语法 ...

  8. 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案

    var threeSumClosest = function(nums, target) { let ans = nums[0] + nums[1] + nums[2]; const len = nu ...

  9. vue 3 打印 print-js

    1.安装 npm install print-js --save 2.引用 import print from 'print-js' 3.编写打印函数 const enterDialog = asyn ...

  10. centos删除安装vsftpd

    准备工作 1.centos 卸载vsftpd 删除原有的vsftpd(卸载前先关闭 vsftpd: systemctl stop vsftpd)[root@localhost ~]# rpm -aq ...