views:

1120

answers:

2

This is the code in my Activity. Initiate an Intent, then a Connection, right?

hello_service = new Intent(this, HelloService.class);
hello_service_conn = new HelloServiceConnection();
bindService( hello_service, hello_service_conn, Context.BIND_AUTO_CREATE);

But my question is...what goes inside the Connection?

   class HelloServiceConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName className,IBinder boundService ) {

        }
        public void onServiceDisconnected(ComponentName className) {

        }
    };

Can someone tell me what code I put into onServiceConnected and onServiceDisconnected?

I just want a basic connection so that my Activity and Service can talk to each other.

Edit: I found a good tutorial, and I can actually close this question, unless someone wants to answer. http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/

A: 

Hi Alex,

I copy those code from LocalServiceBinding.java in the ApiDemos application. You can take a look at the app for more detail)

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service.  Because we have bound to a explicit
        // service that we know is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.
        mBoundService = ((LocalService.LocalBinder)service).getService();

        // Tell the user about this for our demo.
        Toast.makeText(LocalServiceBinding.this, R.string.local_service_connected,
                Toast.LENGTH_SHORT).show();
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        // Because it is running in our same process, we should never
        // see this happen.
        mBoundService = null;
        Toast.makeText(LocalServiceBinding.this, R.string.local_service_disconnected,
                Toast.LENGTH_SHORT).show();
    }
};
Bino
A: 

Hi! im trying to build a rest android client, and i want to use a service to call the rest methods, but should the service send the response back to activity, if yes, should i use a connector to bind the service to the activity?

Max