Android "service" Not Really In Background?
Solution 1:
What is a Service?
Most confusion about the Service class actually revolves around what it is not:
- A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
- A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
This is how you should do (again, taken from the manual & also mentioned by @Jave):
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The
IntentService
class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.
Solution 2:
From the documentation for Service
:
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.
Solution 3:
classDownloadingServiceextendsService
{
...
@OverridepublicvoidonCreate()
{
...
// Start up the thread running the service. Note that we create a// separate thread because the service normally runs in the process's// main thread, which we don't want to block.ThreadnotifyingThread=newThread(null, mTask, "DownloadingService");
notifyingThread.start();
}
privateRunnablemTask=newRunnable()
{
publicvoidrun()
{
// here the job is going to be done
}
};
}
Post a Comment for "Android "service" Not Really In Background?"