Home » Android » android – Database operations in IntentService results into application halt,become unresponsive and giving ANR

android – Database operations in IntentService results into application halt,become unresponsive and giving ANR

Posted by: admin May 14, 2020 Leave a comment


I am using an IntentService in a alarm manager to trigger it after every 15 seconds.
I have to continuously send large amount data to server and receiving large amount of data in response in background.
I have to follow beneath process :

  1. I am reading data from Database through queries.

  2. Then converting it in Json through POJO architecture.

  3. Sending this JSON in request to server using Retrofit Library.

  4. Receiving data in response.

  5. Inserting this data to my database through certain queries if any updation in the database.

Is there any alternate approach? As i am facing ANR.
If data is less, its working fine. But as data size is becoming large, UI halts and Application becomes unresponsive.

How to&Answers:

After a lot of struggle finally i got a solution :

I simply used separate process to work for the intent-service.

using this in “Manifest” file

 <service android:name=".BackgroundSyncDataService"

Description :

The colon prefix in front of the name tells Android that the Service is private to its declaring application. If the colon is not used, the Service would be a global process and can be used by other Android applications.

Running a service in its own process gives it its own memory address space and a garbage collector of the virtual machine in this process does not affect the application process.

Running a service in its own process will not block the application in case the service performs long running operations in its main thread.


To avoid ANR’s there are a few key points to remember when using an IntentService:

  • Don’t do intensive work in onCreate or onStartCommand – both are still UI thread
  • Don’t do intensive work in ResultReceiver
  • Don’t start your own threads in the service – it already does this itself
  • Do your intensive work in onHandleIntent
  • Do design your architecture so the only data you receive is the data that has changed
  • Do avoid moving to another process – as this has CPU and memory overheads (not good for your users)

Another solution is to switch to the other type of Service and start a HandlerThread (Or just start the service itself from a background thread – because a Service runs on the thread which started it)