0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    As soon as your app is closed from Recents, your process will end, and so will the Thread. Services may eventually be stopped by the system, but not immediately. – TheWanderer Dec 17 '18 at 22:20
  • @TheWanderer Not the answer i was looking for. If the app has a service running and removed from Recents,it also ends the service. –  Dec 17 '18 at 22:33
  • @TheWanderer https://stackoverflow.com/a/39313881/1419111 –  Dec 17 '18 at 23:32
  • if you have a Huawei, Oppo or Xiaomi, that's because the battery management kills the process. This isn't a standard Android thing. – TheWanderer Dec 17 '18 at 23:54
  • @TheWanderer I have neither of those and tge link says otherwise. Is there an official doc of what you saying? –  Dec 17 '18 at 23:59
  • by app I meant activity. If you have no service running and you remove the app from Recents, the process will stop. If you have a service, it's not supposed to stop. That's a bug. – TheWanderer Dec 18 '18 at 00:01

2 Answers2

0

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).

Master Fathi
  • 349
  • 1
  • 9
  • 19
  • I just removed an app with a running service from recents and the service stopped it didnt continue. https://stackoverflow.com/a/39313881/1419111 –  Dec 17 '18 at 23:07
0

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274