Android cross-process (APP) Communication (four) - AIDL

Android cross-process (APP) communication (a) - Some examples of
Android across process (APP) Communication (b) - Non-AIDL
Android cross-process (APP) Communications (three) - Proxy and Stub mode
Android cross-process (APP) Communication (four ) - AIDL

What is AIDL

From this series of articles, we know Proxy和Stub模式. In fact, 跨进程通信the so-called AIDL is to help us to achieve Proxy和Stub模式, to help us better package 编码and 译码function. Or the bottom transactand onTransactcalling methods.

Small example

Make a classic Echo program. Program to send word to the service, service to print out.

STEP 1: AIDL interface file (provided Service of APP)

Your service provider which interfaces to allow others to use, you have to make it clear that it does not need to be given at this time to achieve.

  • New com.example.aidlechoservice.aidlPackage
  • Create a regular file , namedIEchoService.aidl
1
2
3
4
5
package com.example.aidlechoservice.aidl;

interface {
String echo(String inStr);
}

If you are using Eclipse, then we'll see after this definition creates gen/com.example.aidlechoservice.aidl/IEchoService.javafile

The second step: implement server-side Stub (providing Service of APP)

Super simple, just a word.

1
2
3
4
5
6
7
IEchoService.Stub mBinder = new IEchoService.Stub() {


public String echo(String inStr) throws RemoteException {
return "echo " + inStr + " at " + sdf.format(new Date());
}
};

So overall this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class EchoService extends Service {

private IEchoService.Stub mBinder;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


public void onCreate() {
super.onCreate();
mBinder = new IEchoService.Stub() {


public String echo(String inStr) throws RemoteException {
return "echo " + inStr + " at " + sdf.format(new Date());
}
};
}


public IBinder onBind(Intent intent) {
return mBinder;
}

}

The third step: the preparation of APP AIDL (APP Service's call)

With the above services are exactly the same, it must be exactly the same, otherwise it will not work.

Step Four: Implement client Proxy (APP Service's call)

Very simple, get a word or

1
IEchoService mService = IEchoService.Stub.asInterface(binder);

layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width= 大专栏  Android跨进程(APP)通信(四) - AIDL4;match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="bind Service" />


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="echo"
android:text="echo" />


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="unbind"
android:text="unbind Service" />


</LinearLayout>

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class MainActivity extends Activity {

private IEchoService mService;
private ServiceConnection mServiceConnection;
private Intent mServiceIntent = new Intent();


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mServiceIntent.setComponent(
new ComponentName("com.example.aidlechoservice", "com.example.aidlechoservice.service.EchoService"));
mServiceConnection = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {

}

@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
mService = IEchoService.Stub.asInterface(binder);
}
};
}

public void bind(View v) {
if (isBinded()) {
return;
}
bindService(mServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}

public void unbind(View v) {
if (!isBinded()) {
return;
}
unbindService(mServiceConnection);
}

public void echo(View v) {
if (!isBinded()) {
return;
}
try {
String result = mService.echo("Hello world!!!");
Log.i("TAG", result);
} catch (RemoteException e) {
e.printStackTrace();
}
}

private boolean isBinded() {
return mService != null;
}
}

project address

Github Address

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11711125.html