繁体   English   中英

如何从Broadcast Receiver启动下载管理器?

[英]How to launch download manager from Broadcast Receiver?

我的应用程序下载大型zip文件(100mb +)。 我使用默认的DownloadManager来方便下载。 Google API文档建议注册BroadcastReceiver并侦听ACTION_NOTIFICATION_CLICKED。 我正在这样做,但我不知道如何从BroadcastReceiver中调用DownloadManager。

我想要做的基本上是浏览器的功能。 当浏览器下载文件并且用户单击DownloadManager通知时,将弹出DownloadManager窗口。 我用什么意图来实现这个目标?

我的代码:

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

    }

找到了我自己的答案。 这样就可以了。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM