1

How to handle background services with Android Oreo?

i had created intentservices and services background but due to android Oreo limitation the services stopped when the app closed

WhatsApp app works in background on android 8.1 how is this possible ?

Check this image:

Also, the broadcast receiver is not working when the app is closed

  • The basics for creating a service can be found on existing questions already: https://stackoverflow.com/questions/4360074/creating-a-service-in-android . If you require additional information, please modify your question to include more detail. – keag Nov 30 '18 at 17:55
  • This is more of a Android then programming question try asking on https://android.stackexchange.com/ – jcubic Nov 30 '18 at 17:56
  • @jcubic No, this is definitely a question about programming Android, which is on topic here and offtopic at android.stackexchange.com. However he needs to more clearly describe his actual question to be answerable – Gabe Sechan Nov 30 '18 at 19:24
  • i mean how to enable the background services to run as whatsapp app on api >=26 – Mohamed abd elaziz Nov 30 '18 at 19:49
  • @GabeSechan it was confusing because OP didn't added any code and was asking about Android feature, I've assumed that it was something about settings. my bad. – jcubic Nov 30 '18 at 23:04

2 Answers2

7

App in Foreground - Best option is to use service or Intent Service if you need to some task when application is open or in the stack.

App in Background - If you want to perform some long running periodic operation then best option is to use Jobservice running in background and Jobscheduler implementation to schedule that jobservice. Android O and above JobScheduler is recommended for background operations.

abhi.nalavade
  • 196
  • 3
  • 10
3

Android background services are described in the Android documentation. Google is trying to limit the freedom apps have to run services in the background for security reasons and to save battery.

You basically have the following options:

1. Foreground Service A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track.

2. Background Service background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service. If your app targets API level 26 or higher, the system imposes restrictions on running background services when the app itself isn't in the foreground. In most cases like this, your app should use a scheduled job instead.

3. Bound Service A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it.

checkmate711
  • 3,301
  • 2
  • 35
  • 45