一 实现
1.工程目录
2.创建AIDL文件
在Service工程中创建了aidl后,直接原样复制一份aidl到client中。
3.工具生成
aidl创建完成后,Rebuild一下工程后,会在build目录下生成aidl对应的java文件:
4.配合类RemoteService实现
关键是mBinder的实现
RemoteService.java
package com.cary.aidl.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* RemoteService
*/
public class RemoteService extends Service {
private static final String TAG = RemoteService.class.getSimpleName();
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
private IManager.Stub mBinder = new IManager.Stub() {
@Override
public void eat() throws RemoteException {
Log.i(TAG, "eat");
}
@Override
public void heart() throws RemoteException {
Log.i(TAG, "heart");
}
};
}
清单文件注册:
注意里面的action,这是给客户端调用时用的动作。
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:theme="@style/AppTheme">
<service android:name=".RemoteService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.cary.aidl.RemoteService"/>
</intent-filter>
</service>
</application>
5.客户端调用
首先要实现ServiceConnection,在连接成功之后,会获得一个IManager的实例,通过这个实例即可调用方法。
package com.cary.aidl.client;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.view.View;
import com.cary.aidl.service.IManager;
/**
* Created by xi
*/
public class MainActivity extends Activity {
private IManager mManager;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mManager = IManager.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mManager = null;
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
}
/**
* 启动服务
* @param view
*/
public void start(View view){
Intent intent = new Intent();
intent.setAction("com.cary.aidl.RemoteService");
startService(intent);
}
/**
* 停止
* @param view
*/
public void stop(View view){
Intent intent = new Intent();
intent.setAction("com.cary.aidl.RemoteService");
stopService(intent);
}
/**
* 绑定
* @param view
*/
public void bind(View view){
Intent intent = new Intent();
intent.setAction("com.cary.aidl.RemoteService");
bindService(intent, conn, BIND_AUTO_CREATE);
}
/**
* 解绑
* @param view
*/
public void unbind(View view){
Intent intent = new Intent();
intent.setAction("com.cary.aidl.RemoteService");
unbindService(conn);
}
public void eat(View view){
try {
mManager.eat();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
二、写AIDL注意事项
1. 客户端和服务端的AIDL接口文件所在的包必须相同
2. 需要一个Service类的配合
转载于:https://my.oschina.net/kun123/blog/871257