如何设置广播接收器

时间:2011-05-25 08:31:55

标签: android android-intent broadcastreceiver intentfilter

我想设置广播接收器在获取广播消息时运行某些功能,在本例中,我想捕获下载的管理器意图:

DownloadManager.ACTION_DOWNLOAD_COMPLETE

我查看了Android API示例,但没有找到方法来执行此操作

3 个答案:

答案 0 :(得分:2)

答案 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>