Skip to content
Advertisement

IntentService is freezing my application UI

In my application, I am using Intentservice to fetch the data from server and store fetched data in to local sqlite db. I am using 5 IntentService to fetch and fill five tables. Data may up to 300 rows for each Intent Service. What should I do?

IntentService Code:

JavaScript

Code to call intentservice:

JavaScript

and intent send me the code when it complete the task

In the onReceiveResult.. resultcode is send through the calling intentservice and when the count goes to 5 then lauching the mainactivity(although this trick is not working i.e. launching mainactivity without complete the task assigned to intentservice).

JavaScript

Advertisement

Answer

Are you absolutely certain that it’s the IntentService that is the root cause of the UI freezes? Intent services are specially designed to run in worker threads in order to offload processing from the main (UI) thread, with one of the main reasons for this being to help prevent UI freezes.

Perhaps try starting your debugging efforts at the UI level. In particular, what provides the ResultReceiver to the IntentService when you start it, and what do you do in the onReceiveResult callback method in that receiver instance?

Other than that, for the activity which you’re experiencing the freezes in, check what kind of operations you’re taking. Loading large amounts of data from a database on the main thread (i.e. without using a Loader or something similar to offload the processing to a worker thread) is a common cause of UI freezes, at least in my experience so far.

Update

I think I’ve worked out what the problem is. There are two main issues, both stemming from how you’re using Volley. When you add a request to the Volley queue, it gets executed asynchronously. That means that the queue method returns immediately. In your intent service code, this means that the service immediately goes on to tell the ResultReceiver that it has finished processing, when actually all it has done is queue the request. All five intent services will do this, meaning that MainActivity will be entered into very quickly. This is the first issue.

The second issue explains the freeze you’re experiencing. Although Volley executes requests on worker threads, it returns the parsed responses to requests on the main thread – see the documentation here. This means that all the response processing you’re doing in the intent service (putting the data into the database, etc.) is actually happening on the main (UI) thread. This explains the freezing.

What you probably want to do here is switch over to using Volley’s RequestFuture instead. This basically turns an asynchronous request into a synchronous one by allowing you to block until the request finishes. To do this, create a future of the appropriate type (JSONObject in your case) and set it as both the listener and error listener for the request. Then, queue the request as you do now, and immediately afterward call call the get method on the future. This method will block until the response has finished processing. It’s okay to do this in an intent service because it runs on a worker thread, not the UI thread.

If the request succeeds you’ll get the data returned and you can execute all of the logic which is currently in your Response.Listener implementation. If an error occurs (i.e. the request fails for some reason), the request future will throw an exception which you can handle to take any appropriate actions.

Using request futures is quite a different approach to using listeners and you may need to change your code quite a bit to get it to work, but it should resolve the issues that you’re seeing.

Hope that helps, and my sincere apologies for not picking up on the bug earlier.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement