Ques about working of IntentService

38 views Asked by At

I am a beginner in Android App Development. I am trying to implement IntentService to run a process in the background when the user taps on the 'Start Service' button in main activity. The process is small, and usually finishes off right after few seconds of tapping the button. But that small process has to be run periodically, even after the app closes. Here is the code:

protected void onHandleIntent(Intent intent)
{
    Log.i(TAG, "The service has now started");
    //getting the source code of a html link
    Log.i(TAG,"Task has been completed");
   }

If I keep the Main Activity opened during the IntentService process, I get both the messages in the Log Cat. But if I close the app right after pressing the 'Start Service' button in Main Activity, I get only the first message i.e. 'The service has now started'. Does this mean that the process doesn't finish if I close the app? Why is this happening? What should I do to overcome this problem?

1

There are 1 answers

0
John Cordeiro On

According to IntentService docs, there is no link between your service and the application main thread.

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

I think you aren't getting the log in LogCat because application closes. But the service are logging without your application tag.