2012-03-13 73 views
9

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

    } 

回答

19

找到我自己的答案。這是訣竅。

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