Question

I am editing the code of an android app that is making GPS calls in a service. LocationListener. It also uses ServiceConnection

In some views the device decides that my application is taking too long to respond, and that the user can either "Force Close" or "Wait". Before this popup appears, the application is still usable by the user, they can scroll, slide, press buttons etc.

I am only assuming this is related to the GPS service as it is running whenever this problem happens.

I heard that this problem has to do with a thread running on the UIthread, instead of a background thread. But I was sure that services run asynchronously in the background thread.

Insight appreciated

Was it helpful?

Solution

Using a service does not necessarily spawn a new thread, the service call runs on it's caller thread. From the android API Service doc at: "Note that services, like other application objects, run in the main thread of their hosting process..".

You can specify the service to run on a different process but best practice is to spawn a new thread in the service.

More on android service at: http://developer.android.com/reference/android/app/Service.html

OTHER TIPS

I fully recommend you to extend AsyncTask,it enables proper and easy use of the UI thread. Allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. You may want to do all the computing in doInBackground method.BTW Force Close or Wait is a classic behavior for this kind of issues. Good Luck!!!

Processing in a service can still cause your application to hang.

The solution you should be looking at implementing is to run any logic that may bog down your activity in a separate thread. This includes things like: Database updates/insertions, Network communication, and any other pieces of long running code.

The AsyncTask is a convenient method for this as you can manipulate the UI in the onPreExecute and the onPostExecute functions.

You can implement an AsyncTask directly in your service as a subclass.

Hoepfully this helps! Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top