编程语言
首页 > 编程语言> > android-为什么我的BroadcastReceiver无法从另一个应用程序接收广播?

android-为什么我的BroadcastReceiver无法从另一个应用程序接收广播?

作者:互联网

App A在其清单(在< application>内)中具有以下BroadcastReceiver:

   
   
  
 

而这个接收者:

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);
}

无论出于什么原因,即使从应用B广播,也不会触发应用A中的onReceive().这可能是什么原因?

编辑和解决方案:我忘记写在广播Intent之前使用过setData()了.确实存在问题:删除setData()后,广播便按预期工作.

解决方法:

最初,我忘记写在广播Intent之前使用过setData().确实存在问题:删除setData()后,广播便按预期工作.

我已经改用Intent元数据使用putExtra()了:

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

标签:android-intent,android-manifest,broadcastreceiver,android
来源: https://codeday.me/bug/20191209/2098015.html