views:

1028

answers:

1

I've been trying to bind a service that was started on boot from an activity. The code for starting on boot was mostly taken from the instant messenger.

This is the AndroidManifest.xml definition for the 3 main components:

 <!-- Receiver -->
 <receiver android:name=".receiver.LifestylePPAutoStarter"
  android:process="android.process.lifestylepp">
  <intent-filter>
   <action android:name="android.intent.action.BOOT_COMPLETED"/>
  </intent-filter>
 </receiver>

 <!-- Service -->
 <service android:name=".service.LifestylePPService"
  android:process="android.process.lifestylepp"
  android:enabled="true"
  android:exported="true">
  <intent-filter>
   <action android:name="edu.gatech.lifestylepp.ILifestylePPService" />
   <action android:name="edu.gatech.lifestylepp.SERVICE" />
  </intent-filter>
 </service>

 <!-- Activity -->
 <activity android:name=".app.LifestylePPActivity"
  android:label="@string/app_name">
  <intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
 </activity>

The receiver starts the service on boot without any problems. However when I try to bind the service from my activity, Context::bindService returns true, but ServiceConnection::onServiceConnected is never called. Also, when I start the service from the activity it works as expected (ServiceConnection::onServiceConnected is called).

+1  A: 

Also, when I start the service from the activity it works as expected (ServiceConnection::onServiceConnected is called).

startService() does not involve a ServiceConnection object.

Get rid of both the android:process="android.process.lifestylepp" lines from your manifest. That may be the source of your difficulty, and more importantly it is very unlikely you really need two processes and all the overhead that requires.

CommonsWare
I know that startService() does not involve a ServiceConnection. What I've been meaning to say is: When I start the service from the activity, I can bind it using bindService(intent, connection, 0) without any problems. The problem only occurs when the service is started from the receiver. Thank you for your suggestion, though! Unfortunately, I already tried running the activity and the service on the same process and it didn't change anything.
kloffy
Add the BIND_AUTO_CREATE flag in your bindService() call.
CommonsWare
Wouldn't that cause a new service to be started? The service is already running, it was created on boot by the receiver. I just want to bind it from the activity.
kloffy
If bindService() does not work without a corresponding startService(), then your service is not started. Period. You may think you are starting it at boot time, but either that is not working, or the way you are trying to bind to the service causes Android to think that you are binding to a different service, or something.
CommonsWare
You are right, the service wasn't running. I feel pretty stupid. It did start on boot but it appears that it was being killed when the application package was deployed. It was dead by the time the activity had launched. I guess the only thing I can say in my defense is that strangely bindService() did return true. Thank you for your help!
kloffy
Yes, updating your package will kill running services. In some cases (e.g., low memory), Android will restart services it kills off at a later point -- updating the package is not one of those cases. This is one of the reasons I really don't recommend starting services at boot time with the expectation of leaving them on all the time. See: http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/
CommonsWare