【问题标题】:How to launch download manager from Broadcast Receiver?如何从广播接收器启动下载管理器?
【发布时间】:2012-03-13 00:33:47
【问题描述】:

我的应用程序下载大型 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???

    }

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多