我怎么能等到DownloadManager完成?

时间:2016-02-09 07:09:02

标签: android android-download-manager

这是我目前使用的代码

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

我无法找到让我等到文件下载的方法。

我也尝试了解如何进行ASync,但无法解决这个问题= /

再次感谢!

2 个答案:

答案 0 :(得分:3)

使用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>

答案 1 :(得分:1)

TextBox textBox = new TextBox();
textBox.DataContext = myValueType;
textBox.SetBinding(TextBox.TextProperty, new Binding("what do I put here?");
sample