HFDO5
HFDO5

Reputation: 129

android communicate between remote service and an activity

I try to create remote service for communicating between client and server. the main idea is starting the service with my main activity when the service will start, it will get the server address and port to open a socket.

I want it to be remote service so other applications will be able to use the same service. the service will keep the connection alive by sending and receiving data to\from the server. it will have the methods to read \ write Int and String. in other words, implement the socket in and out methods...

the problem I'm facing for now, is understanding how the remote service works in android. i started with creating a small service, that only have one method for returning an int. here is some code:

ConnectionInterface.aidl:

    interface ConnectionInterface{
      int returnInt();
    }

ConnectionRemoteService.java:

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

public class ConnectionRemoteService extends Service {
    int testInt;

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
}



@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}

@Override
public IBinder onBind(Intent intent) {
    return myRemoteServiceStub;
}   

private ConnectionInterface.Stub myRemoteServiceStub = new ConnectionInterface.Stub() {
    public int returnInt() throws RemoteException {
        return 0;
    }
};

}

and part of "onCreate" on my main activity:

final ServiceConnection conn = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            ConnectionInterface myRemoteService = ConnectionInterface.Stub.asInterface(service);
        }
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    final Intent intent = new Intent(this, ConnectionRemoteService.class);

later on i have an 2 OnClickListeners that binds and unbind the service:

bindService(intent, conn, Context.BIND_AUTO_CREATE);
unbindService(conn);

the one part that i missing here, is how do i use the methods in the service? for now i have only 1 method that returns an int value. how do i call it? and how do i use other methods that gets values to the service?

thanks, Lioz.

Upvotes: 2

Views: 2080

Answers (1)

David Wasser
David Wasser

Reputation: 95578

When you successfully bind to the service, onServiceConnected() is called with the service binder that you then use to communicate with the service. At the moment you are just putting that in a local variable myRemoteService. What you need to do is to store that in a member variable in your main activity. So define it like this in your main activity:

private ConnectionInterface myRemoteService;

and then, in onServiceConnected() do:

myRemoteService = ConnectionInterface.Stub.asInterface(service);

Later, when you want to use the service's method(s), do something like this:

// Access service if we have a connection
if (myRemoteService != null) {
    try {
        // call service to get my integer and log it
        int foo = myRemoteService.returnInt();
        Log.i("MyApp", "Service returned " + foo);
    } catch (RemoteException e) {
        // Do something here with the RemoteException if you want to
    }
}

Make sure that you set myRemoteService to null when you have no connection to the service. You can do that in onServiceDisconnected().

Upvotes: 0

Related Questions