Note that there are some explanatory texts on larger screens.

plurals
  1. POSynchronizing Calls on Bound Services with Threads
    primarykey
    data
    text
    <p>I got a problem using a <code>BoundService</code> in Android.</p> <p>I've got a wrapper, that works as a client with a <code>BoundService</code>, that should have a send method, that binds to a service, sends a message to it and return an answer returned from the service.</p> <p>It should behave something like this:</p> <pre><code>public Object send(Object toSend) { //bind to service //send the Object //return the answer } </code></pre> <p>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:</p> <pre><code>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; } } } </code></pre> <p>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.</p> <pre><code>new Thread(){ public void run() { Object obj = new Wrapper(getApplicationContext()).send("test","MyServiceAction"); } }.start(); </code></pre> <p>I'm getting the following Exception on the <code>obj.notify()</code> line.</p> <pre><code>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) .... </code></pre> <p>Starting mThread before the synchronized-block of the send-method produces the same error.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload