Service Call Backs To Activity In Android
I have a background service running and a client which interacts with the service. When the client requests for some operation, the service performs it and it should send the resul
Solution 1:
Here is the flow
Create your intent to call a service. You can either startService()
or BindService()
with BIND_AUTO_CREATE
Once the service is bond, it will create a tunnel to talk with it clients which is the IBinder
Interface. This is used by your AIDL Interface implementation and return the IBinder
in
privatefinal MyServiceInterface.StubmBinder=newMyServiceInterface.Stub() {
publicintgetNumber() {
returnnewRandom().nextInt(100);
}
};
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "Service OnBind()", Toast.LENGTH_LONG).show();
return mBinder;
}
Once it returns the mBinder
, ServiceConnection
that you created in the client will be called back and you will get the service interface by using this
mConnection = newServiceConnection() {
publicvoidonServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
publicvoidonServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mService = MyServiceInterface.Stub.asInterface(service);
};
Now you got the mService
interface to call and retreive any service from that
Post a Comment for "Service Call Backs To Activity In Android"