本例子将会介绍远程服务回调接口的用法以及注销回调接口的方法、权限校验、远程服务挂掉后重连的方法等。
直接上代码,远程服务aidl接口及服务代码:
package com.example.aidl4.aidl;
import java.util.List;
import com.example.aidl4.aidl.PersionParcel;
interface IAddPersionListener {
void addPersion(in PersionParcel newPersion);
}
package com.example.aidl4.aidl;
import java.util.List;
import com.example.aidl4.aidl.PersionParcel;
import com.example.aidl4.aidl.IAddPersionListener;
interface IPersionBinder {
String getName(in PersionParcel p);
List<PersionParcel> getAllPersions();
void registerListener(IAddPersionListener listener);
void unregisterListener(IAddPersionListener listener);
}
public class AidlService extends Service {
private static final String mAllowPackages[] = { "com.example.aidl5" };
private AtomicBoolean mIsServiceRunning = new AtomicBoolean(true);
private CopyOnWriteArrayList<PersionParcel> mPersionList = new CopyOnWriteArrayList<PersionParcel>();
private RemoteCallbackList<IAddPersionListener> mListenerLists = new RemoteCallbackList<IAddPersionListener>();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
mPersionList.add(new PersionParcel("zhangsan", 20, "男"));
new Thread() {
@Override
public void run() {
// 每隔30秒添加一人并调用接口中定义的方法
while (mIsServiceRunning.get()) {
SystemClock.sleep(30 * 1000);
PersionParcel persionPa