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"/>

 

 posted on 2021-11-30 11:32  1039781968  阅读(287)  评论(0编辑  收藏  举报