Android的下载管理器完成 [英] Android download manager completed

查看:245
本文介绍了Android的下载管理器完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天每个人,

有关Android的下载管理器的小问题。 这是第一次我正在与它已成功下载多个文件并打开它们。但我的问题是我如何检查如果下载完成。

Small question about the download manager in android. It's the first time i'm working with it and have successfully downloaded multiple files and opened them. But my question is how do i check if the download completed.

这种情况是我下载一个PDF文件并打开它,通常该文件是如此之小,它complets开幕前。但是,如果该文件是稍大如​​何,如果下载管理器打开它之前完成下载看一次。

The situation is i download a PDF file and open it, and usually the file is so small it complets before opening. But if the file is somewhat bigger how do i check if the download manager is finished with the download before opening it.

我如何下载:

Intent intent = getIntent();
        DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
        Uri Download_Uri = Uri.parse(intent.getStringExtra("Document_href"));
        DownloadManager.Request request = new DownloadManager.Request(Download_Uri);

        //Restrict the types of networks over which this download may proceed.
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
        //Set whether this download may proceed over a roaming connection.
        request.setAllowedOverRoaming(false);
        //Set the title of this download, to be displayed in notifications.
        request.setTitle(intent.getStringExtra("Document_title"));
        //Set the local destination for the downloaded file to a path within the application's external files directory
        request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,intent.getStringExtra("Document_title") + ".pdf");
        //Enqueue a new download and same the referenceId
        Long downloadReference = downloadManager.enqueue(request);

我如何打开文件

How i open the file

Uri uri = Uri.parse("content://com.app.applicationname/" + "/Download/" + intent.getStringExtra("Document_title") + ".pdf");
            Intent target = new Intent(Intent.ACTION_VIEW);
            target.setDataAndType(uri, "application/pdf");
            target.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            startActivity(target);

所以某处下载和打开文件我希望有一个if语句来检查它是否应该continu或等待文件之间。

So somewhere between downloading and opening the file i want a if statement to check if it should continu or wait for the file.

谢谢

推荐答案

当下载完成,所以你需要注册一个接收器,当下载完成的下载管理器发送一个广播意图的行动:

A Broadcast intent action sent by the download manager when a download completes so you need to register a receiver for when the download is complete:

要注册接收器

registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

和一个BroadcastReciever处理程序

and a BroadcastReciever handler

BroadcastReceiver onComplete=new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        // your code
    }
};

您还可以创建的AsyncTask来处理大文件的下载

You can also create AsyncTask to handle the downloading of big files

建立某种形式的下载对话框,显示下载后在通知区域,比处理文件的打开:

Create a download dialog of some sort to display downloading in notification area and than handle the opening of the file:

protected void openFile(String fileName) {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(fileName)),"MIME-TYPE");
    startActivity(install);
}

您也可以检查样品链接

<一个href="https://github.com/commonsguy/cw-android/blob/master/Internet/Download/src/com/commonsware/android/download/DownloadDemo.java">Sample code

这篇关于Android的下载管理器完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆