未收到Android本地广播

时间:2018-07-01 01:50:00

标签: android localbroadcastmanager

我想将一个活动的简单sata发送到另一个活动。两者都处于活动状态,并且我已经注册了LocalBroadcastManager。发送部分工作,但接收活动却什么都没有。 BroadcastManager发送数据的部分代码:

myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LocalBroadcastManager broadcaster = LocalBroadcastManager.getInstance(SendingDataPageActivity.this);
            Intent broadcastIntent = new Intent(BROADCAST_INTENT);
            broadcastIntent.putExtra("test, "test");
            broadcastIntent.setAction("test, "test");
            broadcaster.sendBroadcast(broadcastIntent);
            Log.d(TAG, "Broadcast sent..."); });

对于接收者:

public class RecievingDataPageActivity extends AppCompatActivity{
BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "local broadcaster received...");}};

我已经在OnCreate中注册了它:

LocalBroadcastManager.getInstance(this).registerReceiver((receiver),
            new IntentFilter(BROADCAST_INTENT));

但是仍然没有收到任何东西。有帮助吗?

2 个答案:

答案 0 :(得分:0)

删除此行,然后重试。

broadcastIntent.setAction(“ test,” test“);

答案 1 :(得分:0)

您正在Intent构造函数中设置操作:

  broadcastIntent.setAction("test", "test");

但是两行之后,您将其覆盖:

with open('test.py') as f:
  print(f.readlines())

您只能执行一项操作,否则Intent将会丢失。