Android-cross-application binding service (AIDL)

Source: Internet
Author: User

Android-cross-application binding service (AIDL)

In the previous chapter, we recorded the differences and usage of startService in bindService.
To use the bindService method, the activity requires the service to return a Binder object. How can we implement the Binder object in two applications? Here we will discuss the concept of AIDL.

AIDL (Android Interface Definition Language) is an IDL Language used to generate code for interprocess communication (IPC) between two processes on the Android device. If you want to call operations on objects in another process (such as Service) in a process (such as Activity), you can use AIDL to generate serializable parameters.

Let's take a look at how to use it,
First, we need to create an aidl interface in the service1 project.
I am using eclipse, create method, right-click the package name-NEW-File-Name of the. aidl suffix **,
Enter the interface code in the aidl file.

After the file is created, the clear project will produce the corresponding java file under the gen directory.

Step 2
EnterModify onBinder Method for service in service1 Project <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPqOstPrC68jnz8I8L3A + DQo8cHJlIGNsYXNzPQ = "brush: java;"> @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return new IMyService.Stub() { @Override public void basicType() throws RemoteException { // TODO Auto-generated method stub } }; }

So far, a simple aidl is successfully created.

Go back to the service2 project and add button event listening,

 case R.id.button3: bindService(serviceIntent, this, Context.BIND_AUTO_CREATE); break; case R.id.button4: unbindService(this); break;

The service in service1 project is successfully bound to the service2 project.

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.