How to launch download manager from Broadcast Rece

2020-08-17 17:54发布

问题:

My application downloads large zip files (100mb+). I'm using the default DownloadManager to facilitate the download. The Google API docs suggest to register a BroadcastReceiver and listen for ACTION_NOTIFICATION_CLICKED. I'm doing that, but I have no clue how to call the DownloadManager from within the BroadcastReceiver.

What I want to do is basically what the browser does. When the browser downloads a file and the user clicks on the DownloadManager notification the DownloadManager window pops up. What intent do I use to accomplish this?

My Code:

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

Found my own answer. This does the trick.

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


标签: android