Android AIDL remote server usage example

Source: Internet
Author: User

 

Many netizens wrote that they do not understand Android AIDL very well. Here, Android123 provides a simple example to help you understand the powerful remote service design on Android.

 

 

1. Why does AIDL have its advantages?

The AIDL service is more like a Server and can provide services for multiple applications. The use of IDL, similar to the COM component or intermediate language design, allows subsequent developers to implement related operations based on exposed interfaces without having to understand the internal structure, AIDL can work in an independent process.

 

 

2. What are the prerequisites for learning AIDL services?

As an extension of services on Android, first you need to understand the Android Service model. Android Serivice can be divided into two modes: three types, 1. the simplest Service is the onBind method. Generally, broadcast communication is used, which is less efficient. 2. Use the Binder mode to process communication between services and other components, such as Activity. After the Android Development Network prompts you to understand the services in the Binder mode, it is much easier to develop the AIDL remote Service.

 

 

Iii. Specific instances

We use com. android123.cwj. demo as the project name. First, create an ICWJ. aidl file under the com. android123.cwj directory of the project directory. The content is

View sourceprint? 1 package com. android123.cwj;

 

2 interface ICWJ {

 

3 String getName ();

 

4}

 

If the AIDL format is correct, the ADT plug-in Eclipse will automatically generate a Java implementation file under the gen directory of the project.

 

 

The code in Service is as follows:

View sourceprint? 01 public class CWJService extends Service {

 

02 public static final String BIND = "com. android123.cwj. CWJService. BIND ";

 

03 public String mName = "android123 ";

 

04 private final ICWJ. Stub mBinder = new ICWJ. Stub ()

 

05 {

 

06 @ Override

 

07 public String getName () throws RemoteException

 

08 {// rewrite the getName method defined in the AIDL interface and return the content of a member variable mName whose value is ours.

 

09 try

 

10 {

 

11 return CWJService. this. mName;

 

12}

 

13 catch (Exception e)

 

14 {

 

15 return null;

 

16}

 

17}

 

18 };

 

19

 

20 @ Override

 

21 public void onCreate (){

 

22 Log. I ("svc", "created .");

 

23}

 

24

 

25 @ Override

 

26 public IBinder onBind (Intent intent ){

 

27 return mBinder; // here we want to return mBinder instead of null in General Service

 

28}

 

29

 

30}

 

Then define in the AndroidManifest. xml file

View sourceprint? 1 <service android: name = ". CWJService">

 

2 <intent-filter>

 

3 <action android: name = "com. android123.cwj. CWJService. BIND"/>

 

4 <category android: name = "android. intent. category. DEFAULT"/>

 

5 </intent-filter>

 

6 </service>

 

Next we can call it in Activity.

View sourceprint? 01 private ICWJ objCWJ = null;

 

02 private ServiceConnection serviceConn = new ServiceConnection (){

 

03

 

04 @ Override

 

05 public void onServiceDisconnected (ComponentName name ){

 

06}

 

07

 

08 @ Override

 

09 public void onServiceConnected (ComponentName name, IBinder service ){

 

10 objCWJ = ICWJ. Stub. asInterface (service );

 

11}

 

12 };

 

Add the Service binding code to onCreate of Activity

View sourceprint? 1 Intent intent = new Intent ();

 

2 intent. setClass (this, CWJService. class );

 

3 bindService (intent, serviceConn, Context. BIND_AUTO_CREATE );

 

Rewrite the onDestory method of the Activity at the same time.

View sourceprint? 1 @ Override

 

2 public void onDestroy (){

 

3 unbindService (serviceConn); // unbind

 

4 super. onDestroy ();

 

5}

 

The following method can be used to execute the getName of AIDL:

View sourceprint? 1 if (objCWJ = null)

 

2 {

 

3 try {

 

4 String strName = obj. getName ();

 

5}

 

6 catch (RemoteException e)

 

7 {}

 

8}

 

In this way, you can see where the AIDL file is incorrectly formatted.

 

 

 

 

Note: This article comes from the network, and the specific source cannot be investigated.

Related Article

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.