logo

Why do apps run in background on Android?


What are the background apps?

Background apps fall in two categories:

Why is an app running in background?

Let's learn how to investigate the state of background apps using WhatsRunning and PermissionManagerX.

  1. Content Providers

    These apps provide some kind of data to other apps when the latter need. For instance there's a system app named “Contacts Storage” (com.android.providers.contacts) which provides contacts to other apps. So whenever you open an app like “Phone” or “WhatsApp” which needs contacts, the “Contacts Storage” app will automatically run in background.

    In WhatsRunning you can type PROVIDER in the top search bar to find all apps running because of being a content provider:

    Android content provider

    And the detail of the process:

    Android content provider
  2. Background Services (Started)

    Starting with Android 8, only system apps can start background services. For instance “SystemUIService” is always active since it manages user interface for us.

    Android background system app service

    User apps are put in cached state even if they are running a background service. So they cannot perform any task.

  3. Background Services (Bound)

    Like in the case of Content Providers, some apps rely on other apps to perform some tasks. So the latter run background services whenever the former need.

    Say you have My Location and AMap UnifiedNlp Backend apps installed on your device. Whenever you open the former, it binds to the latter as a client to fetch the location, and hence the latter runs a background service:

    Android bound service Android bound service

    As soon as the clients unbind from the service, the service app is stopped and put into cached state.

  4. Foreground Services

    If a user app needs to perform a persistent task in background, it runs a foreground service which shows a notification. For instance, WhatsRunning runs a foreground service to show Battery Stats and PermissionManagerX runs a foreground service for Permission Watcher:

    Android foreground service

    You can see both services running in WhatsRunning:

    Android foreground service
  5. Scheduled Jobs and Alarms

    Apps can request the operating system (using WorkManager, JobScheduler or AlarmManager) to run one-time or repeated tasks at a given interval or at a fixed time. Android starts these apps at the required time. Some of them may also wake up the device from sleep. Others may opt to run only when certain conditions meet, like battery fully charged, internet available etc. Once started, the apps may opt to hold Wake Lock and start a Foreground Service to run a long running task.

    Android scheduled tasks Android scheduled tasks
  6. Cloud Messaging

    Apps like messengers and social media apps need to show notifications in realtime as soon as something new happens on the other side. So these apps use Google's Cloud Messaging (part of Google Play Services) to receive data from internet even when the device is sleeping or the apps are not running in foreground.

    To get a list of such apps, you can use PermissionManagerX. Tap Exclusion Filters -> Disable Filters and on main screen type c2dm.permission.RECEIVE in the top search bar:

    Android Google cloud messaging
  7. Broadcast Receivers

    Some apps are interested in certain events which occur on device e.g. when the device starts, or when the user changes the device language etc. A complete list of such events can be found here. So the apps register a Broadcast Receiver with the operating system. And whenever the requested event occurs, Android (starts and) notifies the app.


Related:

Comments