Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep an Intent service running

I have two examples of Intentservice. One is the Download example in the commonsware book. the other is at http://www.vogella.com/articles/AndroidServices/article.html#servicecommunication_handler. Both of these examples show the service executing a finite task and they both apparently destroy themselves by running to the end of the scope of the onHandleIntent event.

The service I am writing has to have events and listen for things. One is a LocationListener listening for GPS movement. Another makes Posts to a REST service and listens for replys. I want it to run until a time has elapsed or until it was told to quit by the activity that started it.

How do I keep it running? Where, for instance, do I put my implementation of LocationListener? Thanks, Gary

like image 838
Dean Blakely Avatar asked Oct 14 '12 00:10

Dean Blakely


People also ask

Does intent service run in background?

IntentService runs outside the application in a background process, so the process would run even if your application is closed.

Does intent Service run on main thread?

intent service always runs on the worker thread triggered from the main thread.

How do I start an intent Service?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, when user gets the data from intent service it will update.

Why is intent Service deprecated?

Provided since Android API 3, the purpose of IntentService was to allow asynchronous tasks to be performed without blocking the main thread. The deprecation is one of a number of steps introduced starting with Android 8.0 (API 26) to limit the actions that can be performed while an app is in the background.


1 Answers

How do I keep it running?

You don't. IntentService is designed to do a piece of work (or perhaps a few off a queue, if commands happen to come in rapidly), then shut down.

The service I am writing has to have events and listen for things.

Then you should not be using an IntentService. Use a regular Service, with your own background thread(s) as needed.

like image 82
CommonsWare Avatar answered Oct 25 '22 03:10

CommonsWare