Android application within the broadcast LocalBroadcastManager

Usually when we use Android broadcast will be broadcast directly to the AMS system of registration which, due to the busy task of AMS generally may not be able to handle broadcast from us immediately if we are to use in broadcast applications within a single process , the process can be employed LocalBroadcastManager. LocalBroadcastManager Handler uses a broadcast mechanism to deal with the message, and registration to the system is achieved by Binder mechanism, the speed is much faster broadcast the application. However, because of the message Handler mechanism for inter-process with a multi-threaded communications, and therefore can not be used in the application process broadcast across time.

In normal use Broadcast and similar, mainly five steps:

// custom broadcast receivers 1.
public class LocalReceiver the extends the BroadcastReceiver {
public void the onReceive (the Context context, the Intent Intent) {
...
}
}
LocalReceiver localReceiver new new LocalReceiver = ();

// Register 2. broadcast
LocalBroadcastManager.getInstance (context)
.registerReceiver (localReceiver, the IntentFilter new new ( "Test"));
// send a broadcast 4.
LocalBroadcastManager.getInstance (context) .sendBroadcast (new Intent ( "test"));
// 5. unregister broadcast
LocalBroadcastManager.getInstance (context) .unregisterReceiver (localReceiver) ;

And general broadcast as registered at the time of registration of the broadcast radio receiver to LocalBroadcatManager in. When the event is invoked LocalBroadcastManager of sendBroadcast were to occur. Also remember that when not in use to break the broadcast registration.

It is processed in the main message processing thread.

Guess you like

Origin www.cnblogs.com/yxfcnbg/p/11429764.html