如何从广播接收器中启动下载管理器?

11

我的应用程序会下载大型的zip文件(100MB+)。我正在使用默认的DownloadManager来进行下载。Google的API文档建议注册一个BroadcastReceiver并监听ACTION_NOTIFICATION_CLICKED事件。我已经这样做了,但是我不知道如何在BroadcastReceiver中调用DownloadManager。

我想要做的基本上就是浏览器所做的事情。当浏览器下载一个文件并且用户点击DownloadManager通知时,DownloadManager窗口将弹出。我该使用哪个intent来实现这一点呢?

我的代码:

<receiver android:name="com.test.receiver.DownloadReceiver">
  <intent-filter>
     <action android:name="android.intent.action.DOWNLOAD_COMPLETE"></action>
     <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
  </intent-filter>
</receiver>

public class DownloadReceiver extends BroadcastReceiver {

private static final String tag = DownloadReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
    *** code for unzipping removed ***
    }
    else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
        // Open the download manager
        // BUT HOW???

    }
1个回答

21

我找到了自己的答案。这个可以解决问题。

Intent dm = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
dm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(dm);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接