Communication between Android components

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 (intent);

Value example:

Intent intent=getIntent (); String data=intent.getStringExtra (“extra”); TextView tv_data= (TextView) findViewById (R.id.tv_data); tv_data.setText (data);

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.

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 {

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 = “com.my.broad.test”; / / / / / / / / send Intent mIntent = new Intent (ACTION_BROAD_TEST);

Receive the Broadcast instance:

/ / dynamically register broadcast public void registerMessageReceiver () {

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.

 

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:

EventBus.post (event);

The following is an example of EventBus use on the Internet: http://blog.csdn.net/jdsjlzx/article/details/40856535

Http://blog.csdn.net/jdsjlzx/article/details/40856535

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 *