从android中的downloadManager查找下载完成

时间:2015-05-08 04:28:17

标签: android android-download-manager

我正在使用DownloadManager从我的Android应用程序从Web服务器下载文件到设备外部存储。我需要在完成此下载时启动另一个功能。我的文件下载代码如下:

String url = "http://192.168.1.105/download/file.ext";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "files.ext");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);

请建议如何获得onDownloadComplete .....

之类的内容

3 个答案:

答案 0 :(得分:2)

您可以将ACTION_DOWNLOAD_COMPLETE与BroadcastReceiver

一起使用
public class DonwloadCompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // get complete download id
        long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        // to do here
    }
}

答案 1 :(得分:2)

我在DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast receiver receiving same download id more than once with different download statuses in Android发现了我的回答 我的代码是这样的:

private boolean downloadComplete(long downloadId){
        DownloadManager dMgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        Cursor c= dMgr.query(new DownloadManager.Query().setFilterById(downloadId));

        if(c.moveToFirst()){
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));

            if(status == DownloadManager.STATUS_SUCCESSFUL){
                return true; //Download completed, celebrate
            }else{
                int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
                Log.d(TAG, "Download not correct, status [" + status + "] reason [" + reason + "]");
                return false;
            }
        }
        return false;
    }

答案 2 :(得分:0)

public class myAwesomeClass extends Activity{
    public void onButtonClicked(View view){
       if (dummy has row){
           //do something
           //and proceed to next row
        }
          else {
          //something else
            }
        }
    }

请参阅this