Communication between Android components

First, let’s sort out the ways in which we communicate between different components in Andrew.

(Tips: below, in addition to file storage and ContentProvider, generally refers to communication within the same process. If you want to achieve cross process communication, it also needs the help of Messenger or AIDL technology, followed by a detailed introduction of time, temporarily not discussed).

Mode 1: use Intent to pass the value: (between Activity and Activity)

Value example:

Intent intent=new Intent (); intent.putExtra (" extra" " Activity1"); intent.setClass (Activity1.this, Activity2.class); startActivity;

Value example:

Intent intent=getIntent (); String data=intent.getStringExtra (" extra"); TextView tv_data= (TextView) findViewById (; tv_data.setText;

Mode two: use Binder to transmit values (between Activity and Service).

1. define Service

In the Service, define an internal class that inherits from Binder, passing this class, passing the object of the Service to the required Activity, so that the Activity can call the public method and property in the Service, as follows:

Public class MyService extends Service {/ / instantiate the Binder class that you define.

Private final IBinder mBinder = new MyBinder ();

Private String mStr = " I am the knight ";

/ * * * * *

* Custom Binder class, internal class, through this class, let Activity get the object of Service.

* * /

Public class MyBinder extends Binder {

MyService getService () {

/ / return the Service object associated with Activity, so that in Activity, some common methods and common attributes in Service can be invoked.

Return MyService.this;




Public IBinder onBind (Intent intent) {

Return mBinder;


/ * * public method, Activity can make calls * /

Public String getStr () {

Return mStr;


2.Activity binding Service

It is to get the MyService object through the getService of IBinder, and then call the Public method. The code is as follows:

Public class MyBindingActivity extends Activity {

/ / custom Service

MyService mService;


Protected void onCreate (Bundle savedInstanceState) {

Super.onCreate (savedInstanceState);

SetContentView (R.layout.main);



Protected void onStart () {

Super.onStart ();

/ / bind to Service, and call the //onServiceConnected method in mConnetion after binding.

Intent intent = new Intent (this, MyService.class); bindService (intent, mConnection, Context.BIND_AUTO_CREATE);



Protected void onStop () {

Super.onStop ();

UnbindService (mConnection);


* * defines ServiceConnection, which is used to bind Service * /

Private ServiceConnection mConnection = new ServiceConnection () {


Public void onServiceConnected (ComponentName className,

IBinder service) {

/ / MyService has been bound to the IBinder object, calling the method to get the MyService object, then you can call the public method inside.

MyBinder binder = (MyBinder) service;

MService = binder.getService ();



Public void onServiceDisconnected (ComponentName arg0) {



Way three: use Broadcast broadcast transmission value

In fact, it uses Broadcast’s sending and receiving to realize communication.

Send an instance of Broadcast:

Static final String ACTION_BROAD_TEST = "" / / / / / / / / / / / / send Intent mIntent = new Intent (ACTION_BROAD_TEST);

SendBroadcast (mIntent);

Receive the Broadcast instance:

/ / dynamically register broadcast public void registerMessageReceiver () {

MMessageReceiver = new MessageReceiver ();

IntentFilter filter = new IntentFilter ();

Filter.addAction (ACTION_BROAD_TEST);

RegisterReceiver (mMessageReceiver, filter);


Public class MessageReceiver extends BroadcastReceiver {


Public void onReceive (Context context, Intent intent) {

/ / TODO Auto-generated method stub if (intent.getAction ().Equals (ACTION_BROAD_TEST)) {





Mode four: use Application, SharePreference, file storage, database, ContentProvider and so on.

It is to use Application to store some data in a longer life cycle for different activity and other read and write calls, but it is not safe, Application is likely to be recycled, SharePreference and file storage and database are basically stored in the corresponding files, without discussion

Mode five: use the interface:

It is to define an interface that needs to be concerned with the place of the event to implement the interface. Then the event triggered places to register / unregister the controls that are interested in the event. It is the observer pattern, and the problem is obvious, which is often more coupled between different components, and more and more interfaces are also troublesome, space reasons, and unspecific expansion.

To sum up, all kinds of communication methods are more or less problems, such as the way five, the coupling is more serious, especially when the interface is more and more, such as the form of broadcasting, when activity and fragment need to interact, it is not suitable, so we need to use a more simple EventBu. S to solve low – coupling communication between components

Mode six: EventBus:

EventBus class library introduction

EventBus is an optimized Android system class library in publish / bus mode.

Simplifying the communication between components

Decoupling of Event’s senders and receivers can work well in Activities, Fragments, and background threads to avoid complex and error prone dependencies and lifecycle issues that make your code more concise and faster class library smaller (< 50K jar) has been used in greater than 100000000+ already installed Apps. There are some advanced features such as delivery threads, subscriber priorities and so on.

EventBus uses three steps

Define events: public class MessageEvent {/ * Additional fields if needed * /}

Prepare subscribers: eventBus.register (this);

Public void onEvent (AnyEventType event)} / * Do something * /};

Post events: (event);

The following is an example of using EventBus on the Internet:

EventBus’s problem?

Of course, EventBus is not a panacea, and there are some problems in the process of using, for example, because of the convenience of use, it will cause misuse at some time, instead of making code logic more chaotic, for example, some places will circulate to send messages, and so on. In the later chapter, we will carefully study whether there is a better alternative to Eve. The way of ntBus, such as RxJava?

Leave a Reply

Your email address will not be published. Required fields are marked *