2

I got a problem using a BoundService in Android.

I've got a wrapper, that works as a client with a BoundService, that should have a send method, that binds to a service, sends a message to it and return an answer returned from the service.

It should behave something like this:

public Object send(Object toSend)
{   
    //bind to service
    //send the Object
    //return the answer
}

I'm still trying to get this to work. I tried to use Threads and synchronized blocks, but i'm still stuck. The code now looks something like this:

public class Wrapper{

private static Context context;
private Object objToSend;
private Object obj = new Object();

private Messenger mService = new Messenger(new Handler());
final Messenger mMessenger = new Messenger(new IncomingHandler());

class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {

                synchronized (obj) {
                    obj = msg.obj;
                    obj.notify();
                }
        }
    }
}

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
            mService = new Messenger(service);

            Message msg = Message.obtain();
            msg.obj = objToSend;
            msg.replyTo = mMessenger;
            try {
                mService.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
    }

    public void onServiceDisconnected(ComponentName className) {
        Log.d(TAG,"Service disconnected, IBinder");
        mService = null;
    }
};

public Wrapper(Context callerContext)
{
    context = callerContext;
}

public void bind(String action)
{
    Intent intent = new Intent(action);
    context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

class MyThread extends Thread {
    private String action;

    public MyThread (String action)
    {
        this.action = action;
    }

    public void run() {
            bind(action);
    }
}

public Object send(Object toSend, String action)
{   
    objToSend = toSend;
    synchronized (obj) {
        try {
            new MyThread(action).start();
            obj.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return obj;
    }
}
}

For calling send i tried to use it in a separate Thread, if i don't the onServiceConnected will not be called, although the onBind() of the Service is called.

new Thread(){
        public void run()
        {
            Object obj = new Wrapper(getApplicationContext()).send("test","MyServiceAction");
        }
    }.start();

I'm getting the following Exception on the obj.notify() line.

12-15 20:50:10.859: E/AndroidRuntime(22884): Uncaught handler: thread main exiting due to uncaught exception
12-15 20:50:10.859: E/AndroidRuntime(22884): java.lang.IllegalMonitorStateException: object not locked by thread before notify()
12-15 20:50:10.859: E/AndroidRuntime(22884): at java.lang.Object.notify(Native Method)
12-15 20:50:10.859: E/AndroidRuntime(22884): at de.Wrapper$IncomingHandler.handleMessage(Wrapper.java:57)

....

Starting mThread before the synchronized-block of the send-method produces the same error.

Jay89
  • 21
  • 3

0 Answers0