binder-JAVA层使用AIDL

本篇主要介绍JAVA层如何使用AIDL进行binder开发

AIDL编写

实体类

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
package android.moyokoo.test;

import android.os.Parcel;
import android.os.Parcelable;

public class Light implements Parcelable {
String name;
int status;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeInt(this.status);
}

public Light() {
}

protected Light(Parcel in) {
this.name = in.readString();
this.status = in.readInt();
}

public static final Parcelable.Creator<Light> CREATOR = new Parcelable.Creator<Light>() {
@Override
public Light createFromParcel(Parcel source) {
return new Light(source);
}

@Override
public Light[] newArray(int size) {
return new Light[size];
}
};
}

编写实体类的AIDL

1
2
3
package android.moyokoo.test;

parcelable Light;

编写服务接口的AIDL

1
2
3
4
5
6
7
8
9
10
// IFlashLightService.aidl
package android.moyokoo.test;

// Declare any non-default types here with import statements
import android.moyokoo.test.Light;

interface IFlashLightService {
Light getLight();
int open(int status);
}

编写service

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
package android.moyokoo.test;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;

public class FlashLightService extends Service {

private final IFlashLightService.Stub mBinder = new IFlashLightService.Stub() {

@Override
public Light getLight() throws RemoteException {
return new Light();
}

@Override
public int open(int status) throws RemoteException {
return status;
}
};

@Override
public void onCreate() {
super.onCreate();
}

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

声明service

1
2
3
4
5
6
7
8
9
<service
android:name="android.moyokoo.test.FlashLightService"
android:process=":remote"
android:exported="true" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.moyokoo.test.FlashLightService" />
</intent-filter>
</service>

编写客户端

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
package android.moyokoo.test;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
IFlashLightService iFlashLightService;
ServiceConnection serviceConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iFlashLightService = IFlashLightService.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
iFlashLightService = null;
}
};
}

/** * 绑定远程服务 */
private void bindRemoteService(){
Intent intent = new Intent(MainActivity.this, FlashLightService.class);
intent.setAction(IFlashLightService.class.getName());
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}

/** * 解除绑定远程服务 */
private void unbindRemoteService(){
unbindService(serviceConnection);
}
}

看到 iFlashLightService = IFlashLightService.Stub.asInterface(service);

以后看到 Stub.asInterface ,就应该知道,它返回的是一个指向远程服务的对象

拿到服务对象,你就可以为所欲为了