Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 result back to the activity (client).

I know how to invoke the service methods in activity and using call backs we can achieve what I want to do. But I am not able to understand the call back mechanism and code example provided in Api demos (remoteservice).

Could someone explain how this service callback works; or anything which is achievable using simpler mechanism.

like image 759
Ambika Avatar asked Jan 08 '10 08:01

Ambika


People also ask

How do I send a callback from service to activity?

You need to create a BroadcastReceiver in your activity (be sure to register it in onResume() and unregister it in onPause() ) and notify it via a broadcast, providing an Intent .

How do you communicate between service and activity?

This example demonstrates how do I communicate between Activity and Service in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What are callbacks in Android?

Callbacks are all over the place in Android Development. That's simply because they do a job, and they do it well! By definition: A callback is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

How can I communicate between two services in Android?

You have to use BroadcastReceiver to receive intents, and when you want to communicate simply make an Intent with appropriate values. This way you should be able to make a 2-way communication between any component.


1 Answers

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

private final MyServiceInterface.Stub mBinder = new MyServiceInterface.Stub() {
    public int getNumber() {
        return new Random().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 = new ServiceConnection() {

        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }

        public void onServiceConnected(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

like image 142
George Nguyen Avatar answered Nov 09 '22 02:11

George Nguyen