# aidl-demo
**Repository Path**: mengzhang6/aidl-demo
## Basic Information
- **Project Name**: aidl-demo
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2023-12-29
- **Last Updated**: 2023-12-29
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
1. 服务端
```java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import androidx.annotation.Nullable;
import com.example.demo2.IHelloService;
public class RemoteService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
// Return the interface
return binder;
}
private final IHelloService.Stub binder = new IHelloService.Stub() {
@Override
public String sayHello(String name) throws RemoteException {
return String.format("Hello %s", name);
}
};
}
```
```xml
```
```java
Intent intent = new Intent(this, RemoteService.class);
startService(intent);
```
2. 客户端 client2
```java
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IHelloService helloService = IHelloService.Stub.asInterface(service);
try {
String text = helloService.sayHello("morningcat");
v.setText(text);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {}
};
Intent intent = new Intent();
intent.setAction("com.example.service.action");
intent.setPackage("com.example.demo2");
bindService(intent, connection, BIND_AUTO_CREATE);
```
```xml
```
3. 注意点
```gradle
buildFeatures {
aidl true
}
```