【问题标题】:How can I wait until DownloadManager is complete?我怎样才能等到 DownloadManager 完成?
【发布时间】:2016-05-19 01:49:29
【问题描述】:

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

                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);

我找不到等待文件下载的方法。

我也尝试弄清楚如何进行异步,但也无法弄清楚 =/

再次感谢!

【问题讨论】:

标签: android android-download-manager


【解决方案1】:

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

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
        }
    }
}

并将其注册到您的清单中:

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

【讨论】:

  • 它是一个广播接收器。您必须创建一个新类并将其保存为 DownloadBroadcastReceiver.java
  • 好的。对不起,我对 Android 应用程序和 Android Studio 非常迟钝。我应该将第一个代码块放在哪个目录中?我应该在清单中的哪个位置放置第二个代码块?
  • 您可以将DownloadBroadcastReceiver.java 放在您拥有其他java 文件(如MainActivity.java)的同一目录中。 AndroidManifest.xml 声明也是如此,将其放在您的 Activity 声明下方。
  • 你真的很棒。好吧,最后一个愚蠢的愚蠢问题。我怎么知道在我的MainActivity.java 中使用它?
  • 通过注册AndroidManifest 文件,只要DownloadManger 的下载操作完成,就会自动调用接收方的onReceive()。因此,无论您在那之后需要做什么,都将代码写在onReceive()if 块中,如我的回答中所发布的那样。这是写评论以显示通知的地方。
【解决方案2】:

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:

To register receiver

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

and a BroadcastReciever handler

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

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);
}

you can also check the sample link
sample

【讨论】:

    猜你喜欢
    • 2011-12-14
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多