1

I'm currently doing an android application requiring a background service running in another thread. The issue is I want to start it after BOOT_COMPLETE, and don't want it to block my main thread.

Anyone has any idea?

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Yang Ou
  • 11
  • 2
  • You need to provide a use case, what do you mean you don't want to block the main thread? – JoxTraex Jan 29 '12 at 08:30
  • Sorry I didn't make it clear. I have an application containing two services. Both of them have a looper in side. Since only one looper can be associated within one thread, so if I want to get these two services running at the same time, they better be running in different threads. I know there is a way to start them via "new Thread()" implementing the run() method. But how to start them after BOOT_COMPLETE? A broadcastreceiver can't start a thread, am I right? – Yang Ou Jan 29 '12 at 08:46

2 Answers2

1

You have to register a BroadcastReceiver with action BOOT_COMPLETE.

In the onReceive() method of receiver you have to start your Service by using the startActivity() method.

See a similar post here.

Community
  • 1
  • 1
Deepthi Jabili
  • 1,224
  • 1
  • 10
  • 7
  • Yeah, I've done that already with startService() in the onReceive() method. However, my question is, is there anyway to start this service in another thread, so that I can have two service running in different threads doing different stuff after bootup? – Yang Ou Jan 29 '12 at 08:49
0

Look up a broadcast receiver... that is all you need.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • I've already done the braodcastreceiver and successfully started this service. However, it was started in the main thread. So starting it in another thread is the crucial point for me. But anyway, thanks for the reply – Yang Ou Jan 29 '12 at 08:28