Android 进程间通信AIDL Demo
时间: 2023-11-02 15:03:20 浏览: 105
好的,下面是一个简单的 Android 进程间通信 AIDL Demo,包含了一个服务端和客户端。
服务端:
```java
// 定义接口
interface IMyAidlInterface {
int add(int a, int b);
}
// 实现接口
public class MyAidlService extends Service {
private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
```
客户端:
```java
public class MainActivity extends AppCompatActivity {
private IMyAidlInterface mIMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 绑定服务
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.myapplication", "com.example.myapplication.MyAidlService"));
bindService(intent, mConnection, BIND_AUTO_CREATE);
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// 获取接口对象
mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
try {
// 调用服务端接口方法
int result = mIMyAidlInterface.add(1, 2);
Toast.makeText(MainActivity.this, "1 + 2 = " + result, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mIMyAidlInterface = null;
}
};
}
```
在 AndroidManifest.xml 中分别注册服务端和客户端:
```xml
<manifest>
<application>
<service android:name=".MyAidlService"
android:exported="true">
<intent-filter>
<action android:name="com.example.myapplication.IMyAidlInterface" />
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
```
注意在服务端的 AndroidManifest.xml 中设置了 `android:exported="true"`,表示服务对外开放,客户端才能绑定它。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)