0

I have a service that does some heavy work on a background thread. It basically downloads a large zip (>5MB), stores it, unzips it, parses the XML and stores the information in a SQLite database.

While the service is doing its job, I sometimes have to run a quick background task (like logging in the user). However, since the service is very busy (usually extracting the zip file), my short task takes a while to run (and the user has to wait).

Is there a way I could completely pause my service (and it's background AsyncTask) so I can run my new higher-priority task?

Thanks!

Michael Eilers Smith
  • 8,466
  • 20
  • 71
  • 106
  • Do you use simple thread or AsyncTask in your service? Note that on most Android OS versions _all_ AsyncTasks run on the same thread, so if you use AsyncTask in your service for long operation it will block all future AsyncTasks. Which may or may not be the source of your problem (more detail is required). If it's not, another suggestion is to lower the thread of your service instead, the side benefit is that it may even improve UI responsiveness. – Kai Sep 05 '14 at 03:19
  • Fail. App designed to download data before the user is logged in. – danny117 Sep 05 '14 at 05:27
  • @Kai Wow, I didn't know that. I figured every new AsyncTask would spawn a new thread. So how can I lower the thread in my service? – Michael Eilers Smith Sep 05 '14 at 13:44
  • @omegatai I guess you did use AsyncTask in your service, which definitely would block your other AsyncTask operations. You should change your service code to use thread and optionally sets its priority (`Thread.setPriority(Thread.NORM_PRIORITY - 1)`). If this works out for you, let me know and I'll change it into an answer ;) – Kai Sep 05 '14 at 14:56
  • @Kai: Will an AsyncTask have a higher priority than the thread with priority Thread.NORM_PRIORITY - 1? What's the priority of an AsyncTask by the way? – Michael Eilers Smith Sep 05 '14 at 15:37
  • @omegatai if memory serves, AsyncTask has pretty low priority and has to share a max of 10% of the CPU with all the other low priority threads – Kai Sep 06 '14 at 00:56

1 Answers1

1

According to this:

stackoverflow.com/questions/18480206/asynctask-vs-thread-in-android

whay dont you try simple java thread instead of using AsyncTask. Also you can try giving it bigger priority, but in my opinion priorities are not so effective.

Community
  • 1
  • 1
anchor
  • 755
  • 2
  • 8
  • 17
  • Using Java threads directly in Android is not a good practice, because the developer should handle manually the callbacks, and what should be done in the background, which results in lot of boiler plate code. On the other hand using Asynctasks creates also background thread without all the fuss, and overriding its methods onPrexecute doInBackground onPostExecute etc... helps you have a cleaner code and manage callbacks efficiently. – moud Apr 27 '15 at 19:07