Kalyan,

It should not be necessary to keep track of service state on your own. You can call startService "just in case", and then have the service call stopSelf when *it* knows it's done. StartService calls do not nest, that's made clear in the docs.

Now for binding - are you returning a binder from your service's onBind() method? I believe when you create a Service using wizards in Eclipse, the default implementation returns null, which would explain what you are seeing.

Also, make sure you're not calling unBindService until it's time to do so.

-- Kostya

21.12.2010 19:19, Kalyan Akella ?????:
Hi,

I just tested the start & bind service approach and found some really weird stuff going on in the logcat output. Here's what I tried:

Step1: the main activity A is launched.
Step2: A's *onCreate* method attempts to connect with service S (launched in a separate process private to the activity) in the following way:

   1. Reads the SharedPreferences to determine if S is already started
      (using a boolean flag)
   2. If S is not started, invokes /startService/ with intent I (/new
      Intent(this, MyService.class)/)
          * Also sets the flag in SharedPreferences to indicate
            service started
   3. Calls /bindService(intent, this, *0*)/ with the same intent I.
      This step is always executed irrespective of whether S was
      started or not in 2.

Step3: activity A also implements the ServiceConnection interface and it logs the output on service connected/disconnected callbacks.

Now, when I execute these steps in the emulator (v2.2 API level 8), I see the following logcat output (note that all of it is generated as a result of executing A's *onCreate* method only):

12-21 15:14:01.722: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.my.example/.activities.MyActivity } 12-21 15:14:01.893: INFO/ActivityManager(59): Start proc com.my.example for activity com.my.example/.activities.MyActivity: pid=544 uid=10041 gids={3003, 1015} 12-21 15:14:02.543: INFO/MyActivity(544): Calling startServiceIfRequired *<- this is Step 2*
12-21 15:14:02.552: INFO/MyActivity(544): Starting MyService
12-21 15:14:02.572: INFO/ActivityManager(59): Start proc com.my.example:myRemote for service com.my.example/.services.MyService: pid=550 uid=10041 gids={3003, 1015} *<- first time service starts here* 12-21 15:14:02.602: INFO/MyActivity(544): Calling bindService *<- this is Step 2.3* 12-21 15:14:02.993: INFO/ActivityManager(59): Displayed activity com.my.example/.activities.MyActivity: 1168 ms (total 1168 ms)
12-21 15:14:03.223: INFO/MyActivity(550): Inside service onCreate
12-21 15:14:03.223: INFO/MyActivity(550): Inside readServiceMetadata
12-21 15:14:03.243: INFO/MyActivity(550): Done readServiceMetadata
12-21 15:14:03.273: INFO/MyActivity(550): Inside initLocationManager
12-21 15:14:03.353: INFO/MyActivity(550): Done initLocationManager
12-21 15:14:03.353: INFO/MyActivity(550): My service started
12-21 15:14:03.384: INFO/MyActivity(550): Inside service onBind
12-21 15:14:03.393: INFO/MyActivity(544): Inside activity onServiceConnected 12-21 15:14:03.413: INFO/MyActivity(544): Inside activity onServiceDisConnected *<- why ??? somehow android decides to kill this service*
12-21 15:14:03.423: INFO/MyActivity(550): Inside service onUnBind
12-21 15:14:03.423: INFO/MyActivity(550): Inside service onDestroy
12-21 15:14:03.433: INFO/MyActivity(550): My service stopped

As you can notice, right after the call to startService & bindService, android seems to start killing my service S without my activity even calling /unbindService/. I observed the exact same behavior even if I moved this service connection logic to the A's onStart method.

Am I missing something basic here. Will you please help me ? Let me know if any more information is required.

Sincere Regards,
Kalyan

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to