简体   繁体   English

使用Android下载管理器进行多次下载

[英]Multiple downloading using android download manager

I'm using android downloading manager to download video's from web, and this is done successfully and later after completion of download the video as to be encrypted and downloaded video should be deleted. 我正在使用android下载管理器从网络上下载视频,此操作已成功完成,并且在完成视频下载后,将其加密并删除已下载的视频。 everything works fine. 一切正常。 But problem is: 但是问题是:

I start download a file, immediately i click on second link now after completion of 1st link download video is not encrypting and 2nd video is encrypting successfully. 我开始下载文件,完成第一个链接下载视频未加密且第二个视频成功加密后,立即单击第二个链接。

Here is the code 这是代码

dm = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
request = new Request(Uri.parse(dataModel.getOtherData()));
File already=new   File(Environment.getExternalStorageDirectory()+"/Access/"+dataModel.getName()+".mp4");
if(!already.exists()){
down.setVisibility(View.GONE);
stp.setVisibility(View.VISIBLE);
request.setDestinationInExternalPublicDir("/vtemp", dataModel.getName()+".mp4");
enqueue = dm.enqueue(request);
}else {
Toast.makeText(mContext, "File Already Exists", Toast.LENGTH_LONG).show();
                }



BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {

// downloadId =
// intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
// Log.v("dekid",""+downloadId);
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
    downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    // downloadId=dm.enqueue(request);
    Query query = new Query();
    query.setFilterById(enqueue);
    Cursor c = dm.query(query);
    if (c.moveToFirst()) {
        // downloadId=c.getColumnIndex(DownloadManager.COLUMN_ID);
        Log.v("downlad ID", "" + downloadId);
        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
        title=c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
        Log.v("Video Name", ""+title);
        if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
            // request.setShowRunningNotification(false);
            Toast.makeText(context.getApplicationContext(),"Download Successful", Toast.LENGTH_LONG).show();
            String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
            //  Toast.makeText(context, "Download" + uriString,Toast.LENGTH_SHORT);
            Log.v("Encrypt", ""+uriString);     
            stp.setVisibility(View.GONE);
            down.setVisibility(View.VISIBLE);

            encryption task=new encryption();
            task.execute(title);

It is because when you click on the second link before the first has finished downloading, your enqueue variable will take the download id of the second link. 这是因为当您在第一个链接完成下载之前单击第二个链接时,您的入enqueue变量将采用第二个链接的下载ID。

I had a pretty similar problem and resolved it here 我有一个非常类似的问题,并在这里解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM