代码之家  ›  专栏  ›  技术社区  ›  neu242

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

  •  3
  • neu242  · 技术社区  · 14 年前

    应用程序A的清单中有此BroadcastReceiver(在<应用程序(>):

    这个接收器:

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

    我正试图从应用程序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);
    }
    

    编辑(&A); 解决方案 :我忘了在广播之前在Intent上使用setData()。这确实是问题所在:我一删除setData(),广播就按预期工作。

    1 回复  |  直到 14 年前
        1
  •  3
  •   neu242    14 年前

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

    我已切换为对Intent元数据使用putExtra():

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