Android Using a Thread for Background Jobs

422 Views Asked by At

I have read many posts state that doze mode killed a running service at a particular moment e.x link or that they want to execute a long running thread.

I can't understand why you should use a service to do a background job that you know that in some point it will stop eventually.

For instance:

You could use a simple Thread:

 new Thread(new Runnable).start()

and do some work in it. Using this:

  1. In combination with a wake lock, device wont sleep and thread will keep running.
  2. No doze mode restriction (except network but lets say we do local stuff)

So you can do background work with no restriction whatsoever. Although you should use services for these reasons link.

Is this another way (not better of course but a way nonetheless) of doing a background work? Am I wrong?

2

There are 2 best solutions below

1
Master Fathi On BEST ANSWER

There are a lot of ways to do a background job aside of services check this link it may help you pick the best option for your work : Job Scheduler vs Background Service

And services as @TheWanderer said will continue to work event after the app is closed for a period of time unlike a simple thread that will end immediately when the app is closed.

Read this part in the link that you linked

Services are given higher priority than other Background processes and hence it’s less likely that Android will terminate it. Although it can be configured to restart once there is ample resources available again. You should go through the different processes and their priority/important level in the documentation on processes and threads. Assigning them the same priority as foreground activities is definitely possible in which case it’ll need to have a visible notification active (generally used for Services playing music).

0
David Wasser On

If you are running a background thread that you start from an Activity, Android does not know that you are doing background work in the OS Process that is hosting your Activity. Android can kill the OS Process hosting your Activity at pretty much any time. If the user presses the HOME button or takes a phone call or opens a notification and goes to another application, Android can kill off the OS Process at any time. When the user returns to your application, Android will create a new OS Process and recreate all the relevant activities, but your background thread is hopelessly lost. This is the reason that Android has services.

If you start a Service to perform your background processing, the Service will also start background threads, but these are controlled. Your Service tells Android what to do if it kills the Service while it is processing an Intent. Your Service can therefore be informed and restart (or continue) the background processing as necessary. You can also run the Service in a different OS Process from the OS Process running your activities. This will prevent Android from killing the Service if the user removes your app from the list of recent tasks.

With newer Android SDKs there are other mechanisms you can use, like JobScheduler.