我怎样才能等到DownloadManager完成? [英] How can I wait until DownloadManager is complete?

查看:90
本文介绍了我怎样才能等到DownloadManager完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前正在使用的代码

This is the code I'm currently using

                String iosjiUrl = "http://modapps.com/NotoCoji.ttf";
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(iosjiUrl));
                request.setDescription("Sscrition");
                request.setTitle("Somle");
// in order for this if to run, you must use the android 3.2 to compile your app
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                }
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "NotoColorji.ttf");

// get download service and enqueue file
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

                manager.enqueue(request);

我无法找到一种方法来等待文件下载.

I'm not able to find a way for me to wait until the file is downloaded.

我还试图弄清楚如何进行ASync,但无论如何都无法弄清楚=/

I also tried figuring out how to go about doing ASync but couldn't figure that out either =/

再次感谢!

推荐答案

使用 BroadcastReceiver 检测下载何时完成:

Use a BroadcastReceiver to detect when the download finishes:

public class DownloadBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            //Show a notification
        }
    }
}

并将其注册在您的清单中

and register it in your manifest:

<receiver android:name=".DownloadBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
    </intent-filter>
</receiver>

这篇关于我怎样才能等到DownloadManager完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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