9

I have built an android app for collecting mobile and wearable sensor data. I want this app to be running in background 24/7 without being killed by OS. I am aware of all the pro and cons of running it 24/7 but that's my main business requirement.

EDIT: I have made it as foreground service and it works as long as I keep interacting with my phone but if I keep it idle for let's say 4-5 hrs OS kill it despite being listed as foreground service

Gauranga
  • 931
  • 1
  • 9
  • 17
  • 1
    Possible duplicate of [How to make an application run continuously in android?](https://stackoverflow.com/questions/12470967/how-to-make-an-application-run-continuously-in-android) – Mocas Jan 10 '19 at 08:37
  • create a service : check this link https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/services/ – B.mansouri Jan 10 '19 at 08:38
  • 1
    the only way is to use a foreground service – Vladyslav Matviienko Jan 10 '19 at 08:39
  • 1
    @Gauranga, did you ever figure it out? I'm stuck with the same problem here. I'd like the app to stay alive during a few days for data collection from a Bluetooth connected wearable. I also found the foreground service solution, but like you the app gets killed (while I sleep for example) when inactive for a few hours – Simon Corcos Apr 03 '20 at 17:02
  • 2 years and 3 months later, I still have this same problem. The best I could get is to use AlarmManager that periodically (every few minutes) calls a BroadcastReceived which in turn calls a Foreground Service, with ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to avoid Standby Bucketing. In the Service I collect GPS data and light sensor data. The app ran nicely for 5 days and 5 hours, in the background and with no user interaction, but then it got misteriously killed. I already tried all the solutions proposed below, to no avail. – Franco May 01 '21 at 14:51

3 Answers3

6

It's a pain to run a background service after Android Marshmallow.
Doze and StandBy Modes halt all background operations. Even if you use job dispatcher or work manager, the minimum interval for running an operation is 15 minutes, even if you set it to less than that.
So you need to start a foreground service with sticky notifications to do your work. You can take a look at this article to learn how to start working with foreground services. don't forget to put those permissions into your manifest file if your app is targeting android pie and you can rerun the service on phone restart by using a broadcast receiver which listen to this action

<action android:name="android.intent.action.BOOT_COMPLETED" />

and also you can stop it by listening to this action

 <action android:name="android.intent.action.ACTION_SHUTDOWN" />
Ramzy Hassan
  • 926
  • 8
  • 21
  • 1
    I have made it as foreground service and it works as long as I keep interacting with my phone but if I keep it idle for let's say 4-5 hrs OS kill it despite being listed as foreground service – Gauranga Jan 21 '19 at 14:25
  • what is your phone type and OS @Gauranga – Ramzy Hassan Jan 21 '19 at 14:27
  • I am using xiaomi redmi 4a and android version 7.12.N2G47H. – Gauranga Jan 21 '19 at 14:34
  • 1
    OK, this happens due to many reasons non native os sometimes terimenate the foreground service for saving battery so you need to make your app non automatically managed by os, and sometimes os remove older services if many foreground services are running take a look at this https://stackoverflow.com/questions/6645193/foreground-service-being-killed-by-android/14293528 @Gauranga – Ramzy Hassan Jan 21 '19 at 14:46
0

Use Service for collecting data you can run it foreground and it will not be killed by OS

  • Foreground 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. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app. – Юра Околот Jan 10 '19 at 08:49
  • 2
    I have made it as foreground service and it works as long as I keep interacting with my phone but if I keep it idle for let's say 4-5 hrs OS kill it despite being listed as foreground service – Gauranga Jan 21 '19 at 14:26
0

Make the application into a launcher and set it as a default launcher. You can follow this guide here. The idea is the launcher is launched by the OS when the phone boot and it is never killed when system runs out of memory.

tapsey
  • 456
  • 3
  • 14