Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

android - Dynamic BroadcastReceivers: LocalBroadcastManager.registerReceiver vs registerReceiver

I'm trying to receive broadcasts from a service with 2 different receivers. One receiver update's a view so I register it in the activity's onResume method.

When the app is not in the foreground I use the other receiver so I can show a system notification when the background service completes.

The code below is how I'm registering my receivers:

@Override
protected void onPause() {
    // unregister local
    unregisterReceiver(localReceiver);

    // register remote
    registerReceiver(remoteReceiver, filter);
    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    // remove remote receiver
    // since remote is only registered in onPause it won't be registered during the first onResume call
    // so we want to ignore any exceptions
    try {
        unregisterReceiver(remoteReceiver);
    } catch (IllegalArgumentException e) {
        Log.e(LOG_TAG, "No receiver registered, could be first time");
    }

    // add local receiver
    registerReceiver(localReceiver, filter);
    Log.i(LOG_TAG, "resumed. should be registered");
}

The two receiver's are instantiated like this at the top of the Activity class:

BroadcastReceiver localReceiver = new BroadcastReceiver() { ... };

WaitTimeReceiver remoteReceiver = new WaitTimeReceiver();

The Service makes the intent as:

broadcastIntent = new Intent(Support.SERVICE_BR);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);

// later on sends using
sendBroadcast(broadcastIntent);

The Filter in the Activity is made to match:

filter.addAction(Support.SERVICE_BR);
filter.addCategory(Intent.CATEGORY_DEFAULT);

Everything above is working the pause resume functionality works as expected, but my question is why the LocalBroadcastManager was not?

Using LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this) and then calling lbm.registerReceiver(localReceiver) was not receiving any of my broadcasts for either.

Why wasn't the LocalBroadcastManager receiving any of my broadcasts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

BroadcastReceivers registered with LocalBroadcastManager can only receive broadcasts sent with LocalBroadcastManager. Broadcasts sent with an Activity's or Service's sendBroadcast() method cannot be received by LocalBroadcastManager Receivers.

Use the LocalBroadcastManager#sendBroadcast() method instead. For example:

LocalBroadcastManager.getInstance(this).sendBroadcast(intent)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...