下载管理器完成后,为什么广播接收器没有呼叫?

时间:2019-07-08 09:48:03

标签: java android-asynctask broadcastreceiver android-permissions download-manager

我正在设置下载管理器以下载服务器URL的PDF文件。 文件已完全下载,但是广播接收器不会调用打开文件。 我使用AsyncTask和doInBackground类进行下载,并在清单中设置权限。INTERNET,permission.WRITE_EXTERNAL_STORAGE,permission.WRITE_INTERNAL_STORAGE,permission.READ_EXTERNAL_STORAGE。 我在真实设备中检查了我的目录,并且PDF文件已完全下载,没有崩溃。

在OnCreate中

        registerReceiver(onComplete, filter);

在onClick按钮中

                    downloadPdf(v);
                } else {
                    requestStoragePermission(v);
                }

并设置onDestroy

    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(onComplete);
    }

并下载文件

        @Override
        protected Void doInBackground(String... strings) {
            String fileUrl = strings[0];
            String fileName = strings[1];
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File folder = new File(extStorageDirectory, DIRECTORY_PDF_NAME);
            if (!folder.exists()) {
                folder.mkdir();
            }
//            File pdfFile = new File(folder, fileName);
            file_download(fileUrl);
            return null;
        }

        public void file_download(String fileUrl) {

            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + DIRECTORY_PDF_NAME + "/" + "au.pdf");
            if (file.exists()) {
                Intent target = new Intent(Intent.ACTION_VIEW);
                target.setDataAndType(Uri.fromFile(file), "application/pdf");
                target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                Intent intent = Intent.createChooser(target, "انتخاب برنامه");
                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(LoginActivity.this, "عدم موفقیت در نمایش فایل", Toast.LENGTH_SHORT).show();
                }
            } else {
                mgr = (DownloadManager) LoginActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
                Uri downloadUri = Uri.parse(fileUrl);
                DownloadManager.Request request = new DownloadManager.Request(
                        downloadUri);
                request.setAllowedNetworkTypes(
                        DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                        .setAllowedOverRoaming(false)
                        .setTitle("راهنمای ثبت نام")
                        .setDescription("  در حال دانلود..." + "فایل راهنمای ثبت نام")
                        .setDestinationInExternalPublicDir("/" + DIRECTORY_PDF_NAME + "/", "au.pdf");

                try {
                    refid = mgr.enqueue(request);
                    Log.d(TAG, "Checking download status for id: " + refid);

                } catch (ActivityNotFoundException e) {
                    Toast.makeText(LoginActivity.this, "عدم موفقیت در دانلود فایل", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

最后

        public void onReceive(Context ctxt, Intent intent) {

            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + DIRECTORY_PDF_NAME + "/" + "au.pdf");
            Intent target = new Intent(Intent.ACTION_VIEW);
            target.setDataAndType(Uri.fromFile(file), "application/pdf");
            target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            Intent intentPdf = Intent.createChooser(target, "انتخاب برنامه");
            try {
                startActivity(intentPdf);
            } catch (ActivityNotFoundException e) {
            }
        }

1 个答案:

答案 0 :(得分:0)

我用这个解决方案解决了这个问题 我创建一个PDF类,并在其中传输我的下载代码。

public Pdf(Context context, String url, String fileName) {
    this.context = context;
    this.url = url;
    this.fileName = fileName;
    init();
}


private void init() {
    downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    Download_Uri = Uri.parse(url);
}

public void checkAndDownload() {
    if (isStoragePermissionGranted()) {
        if (isExistPdfFile()) {
            openGeneratedPDF();
        }else{
            downloadPdf();
        }

    }
}

public void permissionGranted(int[] grantResults) {
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        downloadPdf();
    }
}

private boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (context.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    } else {
        return true;
    }
}


public BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        isCompeleted = true;
        openGeneratedPDF();
    }

};

private boolean isExistPdfFile() {
    File target = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    file = new File(target, DIRECTORY_PDF_NAME + "/" + fileName + ".pdf");
    if (file.exists()) {
        return true;
    }else{
        return false;
    }
}

private void openGeneratedPDF() {
    if (isExistPdfFile()) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
        Log.e("uri is",uri.toString());
        intent.setDataAndType(uri, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        try {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(context, "برنامه‌ای برای نمایش فایل PDF وجود ندارد", Toast.LENGTH_LONG).show();
        }
    }else {
        Toast.makeText(context,"فایل مورد نظر یافت نشد",Toast.LENGTH_LONG).show();
    }
}

private void downloadPdf() {
    DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(true);
    request.setVisibleInDownloadsUi(true);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, Constant.DIRECTORY_PDF_NAME + "/" + fileName + ".pdf");
    refid = downloadManager.enqueue(request);
    Log.e("OUT", "" + refid);
}

public boolean getIsCompeleted() {
    return isCompeleted;
}

并在我的需求Activity中调用它,我也使用文件提供程序,例如

  Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);