Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid bindService not working
    text
    copied!<p>I'm having an issue with <code>bindService()</code>. I'm trying to do the binding in a constructor, providing an Intent that contains two parcleable extras. The constructor is being called in <code>onResume()</code> and the service, in its <code>onBind()</code> method, parses the two extras and may return <code>null</code> as a result of the parsing.</p> <p>When I first run the app (through the Run in Eclipse) the binding is (expectedly) rejected by the service: the service's <code>onBind()</code> method is called and returns <code>null</code>. However, the <code>bindService()</code> method, on the application side, returns <code>true</code> (it shouldn't, as the binding did not go through!).</p> <p>This gets more problematic when I try the following: I press the HOME button and start the app again (so its <code>onResume()</code> runs again and the app tries to bind to the service again). This time the service's <code>onBind()</code> seems not to be even run! But the app's <code>bindService()</code> still returns <code>true</code>!</p> <p>Below is some sample code that should help you understand my issue.</p> <p>The application side:</p> <pre><code>// activity's onResume() @Override public void onResume() { super.onResume(); var = new Constructor(this); } // the constructor public Constructor(Context context) { final Intent bindIntent = new Intent("test"); bindIntent.putExtra("extra1",extra_A); bindIntent.putExtra("extra2",extra_B); isBound = context.bindService(bindIntent, connection, Context.BIND_ADJUST_WITH_ACTIVITY); log("tried to bind... isBound="+isBound); } </code></pre> <p>The service side:</p> <pre><code>private MyAIDLService service = null; @Override public void onCreate() { service = new MyAIDLService(getContentResolver()); } @Override public IBinder onBind(final Intent intent) { log("onBind() called"); if (intent.getAction().equals("test") { ExtraObj extra_A = intent.getParcelableExtra("extra1"); ExtraObj extra_B = intent.getParcelableExtra("extra2"); if (parse(extra_A,extra_B)) return service; else { log("rejected binding"); return null; } } } </code></pre> <p>The <code>ServiceConnection</code> that I'm using holds the following <code>onServiceConnected()</code> method:</p> <pre><code>@Override public void onServiceConnected(final ComponentName name, final IBinder service) { log("onServiceConnected(): successfully connected to the service!"); this.service = MyAIDLService.asInterface(service); } </code></pre> <p>So, I never get to see the "successfully connected to the service!" log. The first time I run the app (through Eclipse) I'm getting the "rejected binding" log as well as "isBound=true", but from there on I only get the "isBound=true", the "rejected binding" doesn't come up ever again.</p> <p>I suspect that this might have to do with a possibility of Android recognizing that there was a successful bind even when I forced the rejection. Ideally, I would be able to force an 'unbind' too, but that's not possible: I suspect this because, when I kill the app, I'm getting a log that's located in the <code>onUnbind()</code> method of the service (even though there should be no binding in the first place!).</p>
 

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