I am trying to create a client and server type of implementation. I have a background service which need to send data to client when ever a specific method is called using aidl. I am using aidl just because the client and service are in different process.
And make changes at client side when ever a method is invoked.
Can some one help me with this Explained with examples.
1. I am able to bind to service but I am not able to unbind from the service even after calling unbind(Serverconnection) from the client side.
I am trying to use [this book][1] at page number 141 Callbacks with AIDL.
[1]: https://doc.lagout.org/programmation/Android/Android%20Programming_%20Pushing%20the%20Limits%20%5BHellman%202013-11-04%5D.pdf
//////////////////////////////////////////////////////////////////////////////////////////////
//This is how I have implemented it from the provided comments on this questions.
//Server side code to send data through AIDL
// AIDL used to send message across process.
private final IMyAidlInterface.Stub mBinder = new
IMyAidlInterface.Stub() {
@Override
public void registerCallback(final IMListener imlistener) throws
RemoteException {
if (imlistener !=null) mCallbacks.register(imlistener);
}
@Override
public void unregisterCallback(IMListener imlistener) throws
RemoteException {
if (imlistener !=null) mCallbacks.unregister(imlistener);
}
};
//通过处理程序发送字符串消息
public void createHandler(){
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
}
//在另一个线程中调用方法以通过
发送数据 private void register() {
subject.subscribeOn(Schedulers.single())
.observeOn(Schedulers.single())
.subscribeWith(new DisposableObserver<VehicleMessage>() {
@Override
public void onNext(VehicleMessage message) {
streamHandler(message,0);
try {
////////////////////////////////// sends data trhough aidl from a bluetooth data receiver thread./////////////////////////////////
sendToRemote(message);
} catch (Exception e) {
Log.i("error", "" + e);
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
}
);
}
///////////////////////////////////////////////// ////////////客户端或UI端/////////////////////////////////// //////////////////////////
public ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder
iBinder) {
mService = IMyAidlInterface.Stub.asInterface(iBinder);
try {
mService.registerCallback(imListener);
} catch (RemoteException e) {
e.printStackTrace();
}
}
// when disconnected from the server
@Override
public void onServiceDisconnected(ComponentName componentName) {
mService = null;
};
// this is called from a separate thread when ever data is found
public void sendToRemote(VehicleMessage message) {
final int N = service.mCallbacks.beginBroadcast();
for (int i = 0; i < N; i++) {
try {
service.mCallbacks.getBroadcastItem(i).Dataupdate(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
service.mCallbacks.finishBroadcast();
}
///////////////////////增加计数器的值并每秒重置一次,以查找每秒接收的消息数/////// //////////////
///////此处使用的处理程序
private Handler mhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
messageCount++;
if(rFlag==false){
timer.schedule(new speedoThread(),0,1000);
rFlag=true;
}
super.handleMessage(msg);}};