Introduction to Android IPC Programming

Source: Internet
Author: User

1. Introduction to the android IPC Mechanism

1. Introduction to Android processes/threads

In the Android system, an application usually contains multiple components (such as activity and service). These components can be in the same process or in different processes.

By default, multiple components of an application are located in the same thread (main thread) of the same process (main process ), if you want some components to run in a non-master process, you can use androidmanifest. XML file to set the attributes of the corresponding component.

Of course, different applications run in different processes.

When a process is created, a main thread, a logoff object, and Message Queue (MQ) are generated ). The main thread constantly queries message queues and executes messages.

In Android, for an application, the main task of the main thread of the main process is to process UI events. When a UI event occurs, the android framework sends the corresponding UI events to the Message Queue of the main thread. When the main thread finds a new message in the message queue, the message is taken out and processed accordingly, after the processing is complete, continue to execute the logoff object, and continuously query the Message Queue cyclically.

2. Introduction to Android Service

In the Android Application Framework, service is one of the four main components. It is responsible for executing tasks that require long running in the background without directly interacting with the UI.

Other components can start a service, and then the Service continues to run in the background, even if the component that starts the service no longer holds the focus, until the component that starts the service calls the stopservice method or the service calls the stopelf method by itself. In addition, other components can bind themselves to a service, to communicate with the service, even if the service and the component are in different processes.

In fact, there are two ways to get started with a service:

1) started

When a component (such as activity) starts a service by calling the startservice method, the service is called "started ".

Once a service is in "started", the service can continue to run in the background even if the component that starts the service has been destroyed.

Generally, the service that starts to work in this way only executes a separate operation, and does not return the operation result and other information to the component that starts the service. For example, a service is only responsible for uploading or downloading data from the network. when data is downloaded or uploaded, the Service stops itself.

2) bound

When a component binds itself to a service by calling the bindservice method, this service is called bound.

A "Bound" Service provides a C/S interface to allow the component to communicate with the service, such as sending a request to the service and obtaining results from the service, even cross-process communication (IPC ).

The running time of a "Bound" service is the same as that of the component bound to the service.

Of course, a service can be either "started" or "Bound", and a service can have both forms, you only need to implement the onstart and onbind interfaces at the same time.

Whether your application's service is started, bound, or both, all other components, even components in different processes can start the service through an intent, just like starting an activity. Of course, you can also declare your service as private in the androidmanifest. xml file to prevent access from other application components.

To define a service, you only need to derive a class from the service class and reload several methods:

1) onstartcommand

When a component calls startservice, the system calls this method.

2) onbind

This method is called when a component calls bindservice.

3) oncreate

When a service object is created, the system first calls this method.

4) ondestory

When the service is destroyed, the system finally calls this method.

3. Introduction to the android ibinder/binder framework

In the same process, communication between components is very convenient and common function calls can be solved. However, for components in different processes, communication is required, android IPC is required.

For application developers, the android ibinder/binder framework implements Android IPC communication. Of course, the ibinder/binder framework can be used for both intra-process communication (local communication) and inter-process communication (remote communication ).

According to the explanation of ibinder/binder in the android SDK, ibinder/binder is the basic interface for Android remote objects, it is the core part of a lightweight Remote Call mechanism designed by Android to provide high-performance IPC communication. This interface describes the abstract protocol for communication with a remote object.

As mentioned in the android SDK, there are two ways to use the ibinder/binder framework to implement IPC communication:

1) Use the aidl tool provided by the android SDK

Detailed usage will be introduced later

2) directly instantiate the binder class or derive its own binder class from the binder class.

In this way, we often derive our own binder class from the binder class and reload the ontransact method. In this way, we can define a remote object (ibinder interface object ), then implement IPC communication.

The following is a simple example of how to use the ibinder/binder framework provided by Android to implement IPC communication between activities and services in different processes:

1) First, assume that our application contains two components: Activity and service in the main process. In the process named remote, this can be done through androidmanifest. statement in the XML file.

2) Secondly, the service class is derived from the service class:

Public class myservice extends Service {}

In myservice, You Need To derive your own binder class from the binder class and reload its ontransact method.

In addition, the oncreate and onbind methods of the service class must be reloaded.

3) Finally, in the derived activity class, call the bindservice method to bind with the service.

Finally, it should be noted that, through the ibinder/binder framework of Android, both intra-process communication and cross-process communication (IPC) can be implemented ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.