簡體   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