android aidl 接口回调
时间: 2024-12-08 20:12:23 浏览: 20
在Android开发中,AIDL(Android Interface Definition Language)是一种用于实现进程间通信(IPC)的接口定义语言。通过AIDL,应用程序可以定义客户端和服务端之间的接口,从而实现跨进程的方法调用和数据传递。接口回调是一种常用的设计模式,用于在特定事件发生时通知调用者。
以下是如何在Android中使用AIDL实现接口回调的步骤:
### 1. 定义AIDL接口
首先,定义一个AIDL接口,用于描述客户端和服务端之间的通信协议。例如,定义一个回调接口`IMyAidlInterface.aidl`:
```aidl
// IMyAidlInterface.aidl
package com.example.aidl;
interface IMyAidlInterface {
void registerCallback(ICallback callback);
void unregisterCallback(ICallback callback);
void doSomething();
}
```
### 2. 定义回调接口
定义一个回调接口`ICallback.aidl`,用于通知客户端特定事件的发生:
```aidl
// ICallback.aidl
package com.example.aidl;
interface ICallback {
void onEventHappened(String event);
}
```
### 3. 实现服务端
在服务端实现`IMyAidlInterface`接口,并管理回调接口的注册和注销:
```java
// MyService.java
package com.example.aidl;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import java.util.ArrayList;
import java.util.List;
public class MyService extends Service {
private final List<ICallback> callbacks = new ArrayList<>();
private final IMyAidlInterface.Stub binder = new IMyAidlInterface.Stub() {
@Override
public void registerCallback(ICallback callback) throws RemoteException {
if (callback != null && !callbacks.contains(callback)) {
callbacks.add(callback);
}
}
@Override
public void unregisterCallback(ICallback callback) throws RemoteException {
if (callback != null) {
callbacks.remove(callback);
}
}
@Override
public void doSomething() throws RemoteException {
// Perform some operation
notifyEvent("Something happened");
}
};
private void notifyEvent(String event) throws RemoteException {
for (ICallback callback : callbacks) {
callback.onEventHappened(event);
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
}
```
### 4. 实现客户端
在客户端实现`ICallback`接口,并注册到服务端:
```java
// MainActivity.java
package com.example.aidl;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private IMyAidlInterface myService;
private final ICallback callback = new ICallback.Stub() {
@Override
public void onEventHappened(String event) throws RemoteException {
// Handle the event
System.out.println("Event: " + event);
}
};
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = IMyAidlInterface.Stub.asInterface(service);
try {
myService.registerCallback(callback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService(new Intent(this, MyService.class), connection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (myService != null) {
try {
myService.unregisterCallback(callback);
} catch (RemoteException e) {
e.printStackTrace();
}
unbindService(connection);
}
}
}
```
###
阅读全文