0

In Android, how can I run something in the background with the highest priority possible?

I understand I could use a foreground service, but I don't want to show a notification to the user. I need to run something that is CPU-intense but short-lived. It needs to run efficiently (otherwise it will take forever) but it will only take a couple of seconds.

Currently I'm running it on the main thread because it's the only way I can sure it completes within a few seconds on all my test devices. Running it as a (non-foreground) service worked well for most devices but on some devices it was taking close to a minute, which is much more unacceptable than locking up the UI for 2 seconds as it currently does. I also tried AsyncTask but it was taking too long.

rmp251
  • 5,018
  • 4
  • 34
  • 46
  • Can you post the code that "on some devices it was taking close to a minute"? Info about services and binding to them can be found here, http://developer.android.com/guide/components/services.html – petey Dec 01 '15 at 17:18
  • Could you make to find out why it takes close to a min on some devices? One min is a big big time compare to 2 seconds. I guess the reason is something else why it takes a min and I don't think it continuously acquires CPU for a min. Know the reasons then can find out solutions. – cgr Dec 01 '15 at 17:35
  • How about running your service on a separate process ? – duynt Dec 01 '15 at 21:03
  • You could try calling `setPriority(Thread.MAX_PRIORITY)` on the running `Thread`. This will adjust the priority of that `Thread`. However, I don't imagine this will make a difference because your application's process is competing for CPU resources with other running processes, and adjusting the thread priority will only make this thread more important than other threads within the same virtual machine. – David Wasser Dec 02 '15 at 16:45

1 Answers1

0

There is no guarantee that something would be finished in certain amount of time and doesn't matter if you run it on UI thread or in background, because Android is not RTOS.

According to your description the best matching thing that you have to use is IntentService.

Divers
  • 9,531
  • 7
  • 45
  • 88