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. ubuntu 铺满桌面 调整字体 安装firefox中文版

    ubuntu 铺满桌面 安装 open-vm-tools sudo apt-get install open-vm-tools sudo apt-get install open-vm* . rebo ...

  2. python实现自动打卡

    自己也百度了一下,然后写的,分为了三个部分,见三段代码 代码:主程序代码 import timefrom selenium import webdriverfrom private_info impo ...

  3. 安全漏洞之grafana-cve_2021_43798

    漏洞说明 一个可绕过用户登录进行任意文件读取的漏洞 环境搭建 我使用的是vulfocus提供的vulfocus/grafana-cve_2021_43798 ,由vulfocus后台统一管理 利用脚本 ...

  4. NVI手法实现Template Method设计模式

  5. react 富文本编辑器

    5大富文本编辑器今天,富文本编辑器被用于许多应用中,包括简单的博客和复杂的内容管理系统.然而,选择一个并不容易,因为有很多具有不同功能的编辑器. 因此,在这篇文章中,我将评估5个React的富文本编辑 ...

  6. 二进制k8s 集群新增加node 节点

    环境 名称 ip地址 cpu 内存 lgy-k8s-master0021 10.65.0.21 4c 8G lgy-k8s-node0012 10.65.0.12 4c 8G node节点初始化(以新 ...

  7. 052_Lightning

    不支持内容: JavaScript buttons, customizable forecasting, and customizable tabs. 设置部分User选择使用Lightning: 单 ...

  8. pytorch学习笔记(5)--神经网络

    (一)神经网络的骨架 nn.Module import torch from torch import nn class Tudui(nn.Module): def __init__(self): s ...

  9. Sql Server 服务无法正常启动,126异常

    SQL Server 一记 异常信息 方法/步骤 打开[开始菜单]找到软件SQL Server下面的[配置管理器] 可以看到数据库主服务是 '停止' 状态: ![数据库主服务 展开[SQL Serve ...

  10. DNS服务器(简)

    服务端:192.168.182.187 客户端:192.168.182.16 windows客户端:192.168.182.17 1.安装相关服务 yum -y install bind bind-c ...