• Android开发 DownloadManager详解


    前言

      此博客正在整理中。。。

      参考:https://www.jianshu.com/p/e0496200769c

    代码

    请求

    注意 setDestinationInExternalPublicDir方法已经包含根目录路径了,所以直接传入DIRECTORY_DOWNLOADS就可以了,千万不要在导入一个 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() 这样他会自动在创建多层目录

                    DownloadManager downloadManager = (DownloadManager) MainActivity.this.getApplication().getSystemService(Context.DOWNLOAD_SERVICE);
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://dl.hdslb.com/mobile/latest/iBiliPlayer-html5_app_bili.apk"));
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "iBiliPlayer-html5_app_bili.apk");
                    request.setTitle("测试下载");//设置标题
                    request.setDescription("下载中");//设置描述
                    request.addRequestHeader("token","11");//添加头 key: value
                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);//设置网络类型
                    request.setVisibleInDownloadsUi(false);//是否显示下载 从Android Q开始会被忽略
    //                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);//仅在下载中时显示在通知中,完成后会自动隐藏
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//下载中与下载完成后都会在通知中显示
                    id = downloadManager.enqueue(request);//加入队列,会返回一个唯一下载id

    查询

    private void queryFileName(long id){
            DownloadManager downloadManager = (DownloadManager) MainActivity.this.getApplication().getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(id);
            Cursor cursor = downloadManager.query(query);
            if (cursor.moveToFirst()) {
                String file = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI));
                Log.e("调试_临时_log", "this_" + file);
            }
        }

    监听

                    AppDownReceiver appDownReceiver = new AppDownReceiver();
                    registerReceiver(appDownReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    class AppDownReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            Log.e("调试_临时_log", "this_ id = " + completeDownloadId);
            DownloadManager downloadManager = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
            Uri uri = downloadManager.getUriForDownloadedFile(completeDownloadId);
            installApk(context, uri);
    
        }
    
        private void installApk(Context context, Uri uri) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
            } else {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
    
            }
            context.startActivity(intent);
        }
    }
        /**
         * 获取下载状态
         *
         * @param downloadId an ID for the download, unique across the system.
         *                   This ID is used to make future calls related to this download.
         * @return int
         * @see DownloadManager#STATUS_PENDING
         * @see DownloadManager#STATUS_PAUSED
         * @see DownloadManager#STATUS_RUNNING
         * @see DownloadManager#STATUS_SUCCESSFUL
         * @see DownloadManager#STATUS_FAILED
         */
        public int getDownloadStatus(long downloadId) {
            DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId);
            Cursor c = downloadManager.query(query);
            if (c != null) {
                try {
                    if (c.moveToFirst()) {
                        return c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
                    }
                } finally {
                    c.close();
                }
            }
            return -1;
        }

    END

  • 相关阅读:
    Scala中使用fastJson 解析json字符串
    SparkStreaming直连方式读取kafka数据,使用MySQL保存偏移量
    Spark 将DataFrame所有的列类型改为double
    Idea里面远程提交spark任务到yarn集群
    Spark-读写HBase,SparkStreaming操作,Spark的HBase相关操作
    scala之map,List,:: , +:, :+, :::, +++操作
    kafka auto.offset.reset参数解析
    spark-submit提交spark任务的具体参数配置说明
    docker plugin test
    docker ui
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/13299949.html
Copyright © 2020-2023  润新知