在DownloadComplete上没有调用BroadcastReceiver

时间:2016-10-05 07:13:15

标签: android broadcastreceiver

我正在使用DownloadManager下载文件,我想在下载完成时打开它。有了这个提议,我在DialogFragment中有了下一个代码:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    receiverDownloadComplete = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("bla","on receive");

        }
    };
    getActivity().registerReceiver(receiverDownloadComplete,filter);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("bar");
    builder.setMessage("foo");
        builder.setPositiveButton(R.string.open_multimedia, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                int res = getActivity().checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE");
                if (res == PackageManager.PERMISSION_GRANTED) {
                    downloadFile();
                } else {
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            PERMISSION_REQUEST_WRITE_EXTERNAL);
                }
        });
    }
    return builder.create();
}

public void downloadFile() {
    Log.d("bla","download file");
    String folderApp = Environment.getExternalStorageDirectory() + "/folder";
    String folderPath = Environment.getExternalStorageDirectory() + "/folder/game" + gameId;
    File folder1 = new File(folderApp);
    File folder2 = new File(folderPath);
    boolean success = true;
    if (!folder1.exists() || !folder2.exists()) {
        success = folder1.mkdir();
        folder2 = new File(folderPath);
        success = success && folder2.mkdir();
    }
    if (success) {
        String fileString= getFile();
        downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = Uri.parse(getString(R.string.base_url) + getString(R.string.multimedia_path) + fileString);
        Uri newUri = Uri.parse("file://" + folderPath + "/" + fileString);
        File file = new File(folderPath + "/" + fileString);
        if(file.exists()) {
            openMultimedia(folderPath + "/" + fileString);
            return;
        }
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.allowScanningByMediaScanner();
        request.setDescription("...");
        request.setTitle(fileString);
        request.setDestinationUri(newUri);
        request.setVisibleInDownloadsUi(true);
        downloadReference = downloadManager.enqueue(request);
        openMultimedia(folderPath + "/" + fileString);
    } else {
        Log.d("bla","queue download not success");
        Log.e("multimedia","file error");
    }
}

永远不会调用onReceive的{​​{1}}方法,但会下载该文件,我可以将其打开。

0 个答案:

没有答案