简体   繁体   中英

Android local broadcast doesn't receive

I want to send simple sata from an activity to another. Both are active at the time and I have registered LocalBroadcastManager. the sending part work but the receiving activity doesn't get anything. BroadcastManager sending data part of the code :

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

And For receiver:

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

I have registered it in OnCreate:

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

But still doesn't receive anything. Any help?

Remove this line and try again.

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

You are setting your action in the Intent constructor:

 Intent broadcastIntent = new Intent(BROADCAST_INTENT);

But 2 lines after, you are overriding it:

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

You can only have one action so Intent gets lost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM