Skip to content

Everything you need to know about background data collection on Android

Data collection on Android from background environments has become more challenging with recent updates of the Operating System. Especially recording audio data, retrieving the current location via GPS or taking pictures continuously from the background has become challenging. We spent a while trying to figure out how we can continuously record audio data from services over longer periods of time.

Background and Foreground Services, and why Foreground Services still run in the background.

When first dealing with services on Android, you might find the terminology of "background service" and "foreground service" to be misleading. With "background", we associate processes running indefinitely while the user uses other applications, hence they are not in the foreground (i.e., no Activity the App is visible). Hence, you could expect the "foreground service" to be some sort of helper service, that runs while the App is actively used in the foreground (Activity is visible). However, this is not true. Both, background AND foreground services run in the background in the intuitive understanding of the word. However, foreground services are allowed to do more things from the background, such as audio recording, because they continuously display a notification to the user, reminding him that the service is active (i.e., being somewhat in the foreground).

However, foreground services are subject to limitations, too. Certain things like audio recording are only allowed in a foreground service, if the service was started while the App was in the foreground. If you start a foreground service, while the App is minimized (not visible by the user), or restart it from the background after the app has crashed, the foreground services can not record audio. To record audio data (or GPS and images), a foreground service has to be started while the App (Activity) was visible to the User, and only afterward can the activity be minmized. The actual difference between foreground and background services, is that foreground services continuously have to display a notification to the user, so the user knows that the service is running.

Running foreground services the correct way

If you know how, getting the service started the right way, so that it can record audio or location data or images from the background is rather simple. However, it took us quite a while to get it right. We found the following stackoverflow discussion to be particularly helpful.

  • Define the foreground service type to include microphone, location and camera. In the Android Manifest file, you have to declare each service that your applicaiton uses. Further, for foreground services, you can optionally also specify a foregroundServiceType. Only if you declare the type, your service is allowed to access the microphone, for example. For CLAID, our service declaration looks as follows:
    <service android:foregroundServiceType="microphone|location|camera" android:name="CLAIDAndroidServices.CLAIDForegroundService" />
    
  • Start the service while the app is in the foreground (this is crucial). Then and only then has your service the right to access the microphone, camera or location services from the background. You can check whether the app is in the foreground using the following code, for example.:
    ActivityManager.RunningAppProcessInfo processInfo = new ActivityManager.RunningAppProcessInfo();
    ActivityManager.getMyMemoryState(processInfo);
    boolean isInForeground = (appProcessInfo.importance == IMPORTANCE_FOREGROUND || appProcessInfo.importance == IMPORTANCE_VISIBLE);
    
    if(isInForeground)
    {
      // Start service
    }
    

FAQ

Is it still possible to record audio data, images or location from the background on recent Android Versions (>= 12) over longer periods of time?

Yes! If you start a foreground service while the app was in the foreground, and you declare the foreground service to be of type microphone, the service can record audio date indefinitely from the background, even when the app is minmized, as long as it does not get killed or crashes.

Fyi: In our studies, we often have to record audio and location data over longer periods of time such as weeks or even months. This works as long as the service is not restarted or killed. If a service dies, Android typically tries to restart it later. However, it will then be restarted from the background, and hence is not allowed to continue recording. In other words, if the service dies, you loose. We were able to record audio data uninterruptedly for more than one month before our service was terminated.

What can I do to prevent my service from being terminated

The termination of a service can happen due to many different reasons:

  • A crash or segfault occurs (i.e., bug in your code)
  • The service is terminated due to resource saving modes such as battery saving, freeing RAM or preventing overheating; on many Smartphones these can be disabled e.g., via the developer settings or in the settings of the App
  • The device goes into doze mode; this is especially a common problem on Smartwatches running WearOS, as they optimize more aggressively for battery lifetime. However, this can be solved using WakeLocks

Comments