为什么我的BroadcastReceiver不接收来自其他应用的广播?

时间:2010-07-05 11:22:58

标签: android android-intent broadcastreceiver android-manifest

App A在其清单中显示此BroadcastReceiver(在< application>内):

            

这个接收器:

public class RemoteControl extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
      Log.w(TAG, "Look what I did!");
  }
}

我正试图从App B触发这个:

public void onClick(View v) {
  Log.w(TAG, "Sending stuff");
  Intent i = new Intent("app.a.remotecontrol");
  i.setData("http://test/url");
  sendBroadcast(i);
}

无论出于何种原因,App A中的onReceive()即使从App B广播也从未被触发。这可能是什么原因?

EDIT& 解决方案:我忘记写了我在广播之前在Intent上使用了setData()。这确实是问题所在:只要我删除了setData(),广播就会按预期工作。

1 个答案:

答案 0 :(得分:3)

最初我忘记写了我在广播之前在Intent上使用了setData()。这确实是问题所在:只要我删除了setData(),广播就会按预期工作。

我已切换到使用putExtra()代替Intent元数据:

Intent i = new Intent("app.a.remotecontrol");
i.putExtra("url", "http://test/url");
sendBroadcast(i);