Service started multiple times

Put your problem here if it does not fit any of the other categories.

Service started multiple times

Postby val » Wed Apr 02, 2008 2:14 pm

Hello Android-Enthusiasts,

I have a problem involving IntentReceivers and Services. My Architecture is like this:

I have an IntentReceiver: DeleteIntentReceiver. In the onReceiveIntent() Methode of this Class I invoke a background service with startService().
The Services onStart() Methode starts a new Thread to do some network and i/o operations (starting the thread in order to get a more responsive app).

So when I send an Intent, I trigger the invocation of a service via onStart() and there start a new thread that does some time consuming work and should then return.

If I only broadcast those intents from time to time everything works just fine. But I once pressed the sendIntent button alot of times in a row and since then my emulator says:

W/ActivityManager( 508): Scheduling restart of crashed service {abc/abc.package.MyServiceImpl} in 5000ms
W/ActivityManager( 508): Scheduling restart of crashed service {abc/abc.package.MyServiceImpl} in 5000ms
W/ActivityManager( 508): Scheduling restart of crashed service {abc/abc.package.MyServiceImpl} in 5000ms
W/ActivityManager( 508): Scheduling restart of crashed service {abc/abc.package.MyServiceImpl} in 5000ms
W/ActivityManager( 508): Scheduling restart of crashed service {abc/abc.package.MyServiceImpl} in 5000ms
W/ActivityManager( 508): Scheduling restart of crashed service {abc/abc.package.MyServiceImpl} in 5000ms
W/ActivityManager( 508): Scheduling restart of crashed service {abc/abc.package.MyServiceImpl} in 5000ms

and 5000ms later:

D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl
D/ActivityThread(11142): Creating service abc.package.MyServiceImpl

The constructor of MyServiceImpl is called anytime. So there are like 15 instances of my service created. that is not what i want.

As far is I understood Context.startService() is supposed to attach to the running background service or start 1 instance of the service if it is not already running. Is my understanding wrong?

Or is this Architecture wrong by design and should I run Context.startService() not from an IntentReceiver but from an Activity (that would destroy the architectural decoupling which is why I introduced the IntentReceivers on the first place.

I hope you can enlighten me in some way.

Thanks,
Valentin
val
Freshman
Freshman
 
Posts: 9
Joined: Wed Apr 02, 2008 1:54 pm

Top

nevermind

Postby val » Wed Apr 02, 2008 2:31 pm

I restarted the emulator and now can not reproduce said behaviour.

I still feel there is a bug somewhere (in the android apis) because i believe there should never be multiple instances of the same service running.

I wish i would remember when the multiple services started to appear...

Ah and hello to everybody. good work you did here.
val
Freshman
Freshman
 
Posts: 9
Joined: Wed Apr 02, 2008 1:54 pm

Postby sxjthefirst » Wed Apr 02, 2008 2:59 pm

I have faced weird behavior from the emulator too. I think sometimes the adb daemon goes all nuts. Exiting Eclipse killing adb (or stopping it politely) fixes a lot.
sxjthefirst
Developer
Developer
 
Posts: 36
Joined: Wed Mar 12, 2008 7:24 pm

Postby val » Wed Apr 02, 2008 3:37 pm

sxjthefirst wrote:I have faced weird behavior from the emulator too. I think sometimes the adb daemon goes all nuts. Exiting Eclipse killing adb (or stopping it politely) fixes a lot.


Hmmm well the same issue just resurfaced. I will investigate this problem deeper now. In the meantime if anybody has an opinion on whether i misuse the android components for such an architecture i would be glad to hear them.
val
Freshman
Freshman
 
Posts: 9
Joined: Wed Apr 02, 2008 1:54 pm

Postby sxjthefirst » Wed Apr 02, 2008 3:54 pm

I can't see anything wrong with the idea of calling an IntentReceiver to start a service. In fact the sample code in APIDemos that come with android install uses the same architecture. Look in <android install dir>\samples\ApiDemos and the AlarmService example if you haven't already. Your way looks good.
sxjthefirst
Developer
Developer
 
Posts: 36
Joined: Wed Mar 12, 2008 7:24 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests