我想设置广播接收器在获取广播消息时运行某些功能,在本例中,我想捕获下载的管理器意图:
DownloadManager.ACTION_DOWNLOAD_COMPLETE
我查看了Android API示例,但没有找到方法来执行此操作
答案 0 :(得分:2)
你应该先阅读:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
然后看这里的例子。
博文: http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-ii-intent-receivers/
答案 1 :(得分:2)
试试这个:
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE) ){
// do something
}
}
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
答案 2 :(得分:0)
您可以创建一个继承自BroadcastReceiver
:
public class MyDownloadCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
然后在您的应用程序清单中注册此类,如下所示:
<receiver android:enabled="true"
android:name="MyDownloadCompleteReceiver"
android:label="downloadCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>