Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't bind an Activity to Service
    text
    copied!<p>I'm having trouble getting the onServiceConnected() method to run, which means that it's not binding my activity to the service. </p> <p>It's probably something simple that I've missed out - but I've tried quite a few times - starting from scratch.</p> <p>Here we go...</p> <p><strong>My Service Class</strong></p> <pre><code>import android.app.Service; import android.content.Intent; import android.os.IBinder; public class QuickService extends Service { private final IBinder mBinder = new QuickBinder(this); @Override public IBinder onBind(Intent intent) { return mBinder; } } </code></pre> <p><strong>My Binder Class</strong></p> <pre><code>import android.os.Binder; public class QuickBinder extends Binder { private final QuickService service; public QuickBinder(QuickService service){ this.service = service; } public QuickService getService(){ return service; } } </code></pre> <p><strong>And... The Activity trying to bind to the service.</strong></p> <pre><code>import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; public class QuickActivity extends Activity { QuickService mService; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_connecting); } @Override protected void onStart() { super.onStart(); Intent intent = new Intent(this, QuickService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); // Unbind from the service unbindService(mConnection); } /** Defines callbacks for service binding, passed to bindService() */ private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { Logger.d("Connected!!! :D"); // We've bound to LocalService, cast the IBinder and get LocalService instance QuickBinder binder = (QuickBinder) service; mService = binder.getService(); } @Override public void onServiceDisconnected(ComponentName arg0) { } }; } </code></pre> <p>Also, the Service defined in the manifest file - in case you thought that was the problem.</p> <pre><code>&lt;service android:name=".QuickService"&gt;&lt;/service&gt; </code></pre> <p>So, What am I doing wrong here? Why isn't the onServiceConnected() method being called?</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