调用系统下载服务下载文件。监听下载完成拿到downloadid和filename

public class DownLoadAPK {
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static long downloadAPK(DownloadManager downloadManager, String apkUrl, String name, String desc){


        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
        request.setDestinationInExternalPublicDir("zhnet", name+".apk");//表示设置下载地址为sd卡的Trinea文件夹,文件名为MeiLiShuo.apk。
        request.setTitle(name);//设置下载中通知栏提示的标题
        request.setDescription(desc);//设置下载中通知栏提示的介绍
        request.setVisibleInDownloadsUi(true);  //设置显示下载界面
        request.setMimeType("application/vnd.android.package-archive");
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//表示下载进行中和下载完成的通知栏是否显示。
        // 默认只显示下载中通知。
        // VISIBILITY_VISIBLE_NOTIFY_COMPLETED表示下载完成后显示通知栏提示。VISIBILITY_HIDDEN表示不显示任何通知栏提示,
        // 这个需要在AndroidMainfest中添加权限android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.

//        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);//表示下载允许的网络类型,默认在任何网络下都允许下载。
        //有NETWORK_MOBILE、NETWORK_WIFI、NETWORK_BLUETOOTH三种及其组合可供选择。
        //如果只允许wifi下载,而当前网络为3g,则下载会等待。

//        request.setAllowedOverRoaming(true);//移动网络情况下是否允许漫游。

//        request.setMimeType("application/cn.trinea.download.file");//设置下载文件的mineType。
        // 因为下载管理Ui中点击某个已下载完成文件及下载完成点击通知栏提示都会根据mimeType去打开文件,所以我们可以利用这个属性。
        // 比如上面设置了mimeType为application/cn.trinea.download.file,
        // 我们可以同时设置某个Activity的intent-filter为application/cn.trinea.download.file,用于响应点击的打开文件。

//        request.allowScanningByMediaScanner();//表示允许MediaScanner扫描到这个文件,默认不允许。

        //request.addRequestHeader(String header, String value)
        //添加请求下载的网络链接的http头,比如User-Agent,gzip压缩等

        return downloadManager.enqueue(request);
    }
}

上面的工具类有详细注释说明,暂时搜集了这么些功能,下面是监听系统下载完成系统发送广播,监听广播获取下载的详细地址加文件名和id

public class DownloadReceiver extends BroadcastReceiver {
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onReceive(Context context, Intent intent) {
        long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

        Log.d("=====", "下载的IDonReceive: "+completeDownloadId);

        DownloadManager manager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
        if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())){
            DownloadManager.Query query = new DownloadManager.Query();
            //在广播中取出下载任务的id
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            query.setFilterById(id);
            Cursor c = manager.query(query);
            if(c.moveToFirst()) {
                //获取文件下载路径
                String filename = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
                //如果文件名不为空,说明已经存在了,拿到文件名想干嘛都好
                if(filename != null){
                    Log.d("=====", "下载完成的文件名为:"+filename);
                    //     /storage/emulated/0/zhnet/T台魅影.apk

                    //执行安装
                    Intent intent_ins = new Intent(Intent.ACTION_VIEW);
                    intent_ins.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent_ins.setDataAndType(Uri.parse("file://" + filename),"application/vnd.android.package-archive");
                    context.getApplicationContext().startActivity(intent_ins);
//                    filename = filename.substring(filename.lastIndexOf("/")+1, filename.lastIndexOf("."));
//                    Log.d("=====", "截取后的文件名onReceive: "+filename);
                }
            }
        }else if(DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())){
            long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
            //点击通知栏取消下载
//            manager.remove(ids);
//            Toast.makeText(context, "已经取消下载", Toast.LENGTH_SHORT).show();

        }
    }
}

最后记得改清单文件

        <!--监听系统下载完成的广播-->
        <receiver android:name="com.zhnet.AD.broadcast.DownloadReceiver">
            <intent-filter>
                <action android:name="DownloadManager.ACTION_DOWNLOAD_COMPLETE"/>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
                <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
            </intent-filter>
        </receiver>
原文地址:https://www.cnblogs.com/tero/p/5403192.html