如何从广播接收器推出下载管理器? [英] How to launch download manager from Broadcast Receiver?
问题描述
我的应用程序下载较大的zip文件(100MB +)。我使用的是默认的下载管理器,方便下载。在谷歌的API文档建议注册一个BroadcastReceiver,并听取了ACTION_NOTIFICATION_CLICKED。我这样做,但我不知道如何从的BroadcastReceiver中调用下载管理器。
我想要做的基本上是浏览器一样。当浏览器下载的文件,并在用户点击了下载管理通知的下载管理窗口弹出。我用什么意图做到这一点?
我的code:
<接收器的Android版本:NAME =com.test.receiver.DownloadReceiver>
<意向滤光器>
<作用机器人:名称=android.intent.action.DOWNLOAD_COMPLETE>< /作用>
<作用机器人:名称=android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED/>
&所述; /意图滤光器>
< /接收器>
公共类DownloadReceiver扩展的BroadcastReceiver {
私有静态最后字符串变量= DownloadReceiver.class.getSimpleName();
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
串动= intent.getAction();
如果(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(动作)){
*** $ C $下解压删除***
}
否则如果(DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(动作)){
//打开的下载管理器
//但如何???
}
找到我自己的答案。这是卓有成效的。
意图DM =新的意图(DownloadManager.ACTION_VIEW_DOWNLOADS);
dm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(DM);
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???
}
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);
这篇关于如何从广播接收器推出下载管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!