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 :
I am reading data from Database through queries.
Then converting it in Json through POJO architecture.
Sending this JSON in request to server using Retrofit Library.
Receiving data in response.
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.
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.
IntentService is an extension of the Service component class that handles asynchronous requests (expressed as Intent s) on demand. Clients send requests through Context.
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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With