'Is it a good thing to starting service in new thread from activity
new Thread(new Runnable(){
@Override
public void run () {
Intent firstServiceIntent=new Intent(getApplicationContext(),MyService.class);
}
}).start();
I have to perform background location updates in the service. I've used LocationListner in the service and it calls onLocationChanged() frequently, which can block my UI thread. If I use create thread in onLocationChanged(), every-time this method is called a new thread would be spawned. So, I'm thinking of creating the entire service in new thread instead of creating thread in onLocationChanged() . Is this a good solution or what should be the best way? Any help is appreciated.
Solution 1:[1]
A service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise.
However, launching a service from a different thread looks ugly. You can create a new thread inside the service.
Solution 2:[2]
If you are using IntentService component of android , it will automatically take care of your background processing in onHandleIntent(...) you can try...
class YourService extends IntentService
else if you use Service like
class YourService extends Service
you can use your custom thread in onCreate()
ELSE
The service will run in the MainThread
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bertram Gilfoyle |
| Solution 2 |
