Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 834
Heena Arora Avatar asked Jun 21 '16 10:06

Heena Arora


People also ask

What is the difference between service and IntentService How is each used?

Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service. IntentService creates a queue that passes one intent at a time to onHandleIntent() . Thus, implementing a multi-thread should be made by extending Service class directly.

What is the purpose of the IntentService class?

IntentService is an extension of the Service component class that handles asynchronous requests (expressed as Intent s) on demand. Clients send requests through Context.

Does IntentService run on background thread?

If the Background task is to be performed for a long time we can use the intent service. Service will always run on the main thread. intent service always runs on the worker thread triggered from the main thread.

How do I get context in IntentService?

How do I get the context in an IntentService? Here is a snippet of the receiver and the scheduling service. public class BackupAlarmReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, BackupSchedulingService.


1 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"
        android:process=":my_process">
  </service>

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.

like image 157
Heena Arora Avatar answered Sep 25 '22 12:09

Heena Arora