Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have revised my solution to moving my LocalBinder to its own file as follows:</p> <pre><code>public class LocalBinder extends Binder implements ILocalService { @Override public int getStatusCode() { return LocalService.statusCode; } @Override public void startRcvThread(Handler handler) { Thread thread = passRcvHandlerToRcvThread(handler); thread.start(); } public Thread passRcvHandlerToRcvThread(Handler handler) { Message msg = Message.obtain(); msg.what = LocalService.emptyMsg; Thread thread = new RcvThread(handler, msg); return thread; } } </code></pre> <p>As you can see I've deleted the getService() method completely because I don't need it. To use the methods defined in LocalBinder above, an activity must define onServiceConnected() and its unimplemented methods onServiceConnected() and onServiceDisconnected() in a manner similar to this:</p> <pre><code>private ILocalService mBoundService; private boolean mIsBound; 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 = (ILocalService)service; int statusCode = mBoundService.getStatusCode(); Log.d("Binding.java","called onServiceConnected. statusCode: " + statusCode); Toast.makeText(Binding.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; Log.d("Binding", "called onServiceDisconnected"); Toast.makeText(Binding.this, R.string.local_service_disconnected, Toast.LENGTH_SHORT).show(); } }; </code></pre> <p>With mBoundService declared and defined as above I now have an IBinder with which to invoke methods defined in LocalBinder above.</p> <p>And as before there are some things to note that I'm not sure about but seem to work:</p> <ul> <li><p>In LocalBinder "getStatusCode()" returns a static "LocalService.statusCode."</p></li> <li><p>In LocalBinder "startRcvThread()" invokes "pasRcvHandlerToRcvHandler()" which needs a static "LocalService.emptyMsg."</p></li> </ul> <p>I'm always concerned about using statics but logcat doesn't seem to be complaining about any leaks. I know that does not mean there aren't any. So I hope someone finds this useful.</p>
    singulars
    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.
 

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