AIDL是Android Interface definition language的缩写
下面是aidl通信的实例代码
1、aidl接口定义
(1)callback
package com.test.aidltest;
interface InfTestCallback {
void onAidlTestCallback(String msg);
}
(2)Service
package com.test.aidltest;
import com.test.aidltest.InfTestCallback;
interface InfTestService {
void aidlInit(in InfTestCallback callback);
void aidlUnInit(in InfTestCallback callback);
}
2、Servier端
(1)Service
package com.study.aidlserver;
public class AidlTestService extends Service {
private String TAG = "AidlTestServer";
public final RemoteCallbackList<InfTestCallback> mCallbacks = new RemoteCallbackList<InfTestCallback>();
private InfTestService aidlTestServer = new InfTestService.Stub() {
@Override
public void aidlInit(in InfTestCallback callback) throws RemoteException {
Log.d(TAG, "aidl init ...");
if (null == callback) {
Log.e(TAG, "register Callback is null !");
return;
}
mCallbacks.register(callback); //注册回调
callback.onAidlTestCallback("aidl init success");
Log.d(TAG, "aidl init end");
return;
}
@Override
public void aidlUnInit(in InfTestCallback callback) throws RemoteException {
Log.d(TAG, "aidl uninit ...");
mCallbacks.unregister(callback);
Log.d(TAG, "aidl uninit end");
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return (IBinder)aidlTestServer;
}
}
(2)activity
package com.study.aidlserver;
public class MainActivity extends AppCompatActivity {
public AidlTestServer aidlTestServer = null;
private final Timer timer = new Timer();
private TimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (null == aidlTestServer) {
aidlTestServer = new AidlTestServer();
}
task = new TimerTask() {
@Override
public void run() {
if ((null != aidlTestServer.mCallbacks) && (aidlTestServer.mCallbacks.getRegisteredCallbackCount() <= 0)) {
synchronized (aidlTestServer.mCallbacks) {
aidlTestServer.mCallbacks.beginBroadcast();
int number = aidlTestServer.mCallbacks.getRegisteredCallbackCount();
for (int i = 0; i < number; i++) {
try {
if (aidlTestServer.mCallbacks.getBroadcastItem(i) == null) {
continue;
}
aidlTestServer.mCallbacks.getBroadcastItem(i).onAidlTestCallback("aidl test callback");
} catch (DeadObjectException e) {
if (aidlTestServer.mCallbacks.getBroadcastItem(i) != null)
aidlTestServer.mCallbacks.unregister(aidlTestServer.mCallbacks.getBroadcastItem(i));
} catch (RemoteException e) {
e.printStackTrace();
}
}
aidlTestServer.mCallbacks.finishBroadcast();
}
}
}
};
timer.schedule(task, 2000, 2000);
}
}
3、Client端:
package com.study.aidlclient;
public class MainActivity extends AppCompatActivity {
private InfTestService testServiceInterface = null;
private String TAG = "AidlTestClient";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindTestService(this);
testServiceInterface = InfTestService.Stub.asInterface();
if (null == testServiceInterface) {
Log.d(TAG, "testServiceInterface is null");
return;
}
}
public ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected name "+name);
if (null == service) {
Log.d(TAG, "service is null");
return;
}
testServiceInterface = InfTestService.Stub.asInterface(service);
if (null == testServiceInterface) {
Log.d(TAG, "testServiceInterface is null");
return;
}
try {
if (null != testServiceInterface) {
testServiceInterface.aidlInit(mInfTestCallback);
} else {
Log.d(TAG, "init: testServiceInterface is null");
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected name "+name);
if (null != testServiceInterface) {
testServiceInterface.aidlUnInit(mInfTestCallback);
} else {
Log.d(TAG, "init: testServiceInterface is null");
}
}
};
public boolean bindTestService(Context context) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.study.aidlserver","com.study.aidlserver.AidlTestService"));
boolean ret = context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
Log.d(TAG, "bindTestService: ");
return ret;
}
public void unBindTestService(Context context) {
context.unbindService(connection);
}
private InfTestCallback mInfTestCallback = new InfTestCallback.Stub() {
@Override
public void onAidlTestCallback(String msg) {
Log.d(TAG, "AidlTestCallback = "+msg);
}
};
}