4

We have an application that runs almost forever with a foreground service, while using a notification on the system tray, which is the normal initialization. The app simply depends on this service. On every device we tested, the service keeps running even the task is removed, but on Xiaomi devices, after swiping from the recents, it suddenly stops, then starts again depending on how ActivityManager decides to re-open the service. We're getting logs from Xiaomi devices (Xiaomi MI9 on this case) such as:

Scheduling the restart of the crashed service: com.example.myapp/.MyService in 1000ms

This shouldn't happen, but it does. And every time we open the app and close it from the recents, the 1000ms part keeps increasing to 4000ms, 16000ms, 64000ms and so on. I don't think it has a limit, and 64 seconds is already too long for a foreground service to restart that is crucial for the app. So, I'm searching for methods to add our app as an exception or something, but the only thing I found is this: https://dontkillmyapp.com/xiaomi

If the app is killed with the X button on the recents screen, then that's even worse as I've noticed the device kills all the services and schedules them to restart in 10 second gaps. I think ours were scheduled to start 3 hours after, which destroys the purpose of the app.

The current solution that we're using is to warn the user about this issue and redirect to this link, in order to add our app to exceptions, enabling Autostart and so on. But, we're aware that almost nobody will do this, so we're looking for a solution that can be achieved programmatically.

A little code that demonstrates how we register the service to manifest and how we start it. (The demonstration is simpler than the original, but describes the main logic.)

Manifest part:

<service android:name=".MyService"
    android:stopWithTask="false" />

Starting the service part:

// Starts the service as foreground.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    context.startForegroundService(new Intent(context, MyService.class));
else
    context.startService(new Intent(context, MyService.class));

Posting the notification part:

// Post the notification on both onCreate and
// onStartCommand so we can only hope that 
// the app won't throw the unavoidable exception
// which occurs 5 seconds after calling
// Context.startForegroundService().

@Override
public void onCreate()
{
     super.onCreate();

     // Handles how the notification
     // is shown, content is not important.
     // Calls startForeground inside.
     showNotification();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    showNotification();

    // Some other service code that is irrelevant

    // Return START_STICKY so we can ensure that if the
    // service dies for some reason, it should start back.
    return START_STICKY;
}

I think everything is done correctly as this only happens on Xiaomi devices, but we couldn't find a solution about keeping this service alive. Is anyone else experiencing the same thing? How should we proceed so our service doesn't die? Thanks for all the help.

Furkan Yurdakul
  • 2,801
  • 1
  • 15
  • 37
  • 1
    *Is anyone else experiencing the same thing?* yes, everyone. Blame xiaomi. You can't really do anything next to what you're already doing. Check https://dontkillmyapp.com/ – Tim Sep 17 '19 at 14:13
  • @Tim Castelijns Yeah I know it says there is nothing to do on the development side, but still we need to figure this out one way or the other. – Furkan Yurdakul Sep 17 '19 at 14:15
  • not all problems have solutions. As far as I know you're wasting your time. Maybe someone else knows a hacky workaround. Don't get your hopes up though – Tim Sep 17 '19 at 14:18
  • 2
    I had the same issue. You can get permissions like [this](https://stackoverflow.com/questions/59442078/background-services-are-restricted-in-xiaomi-and-oppo) – Behrad Ranjbar Dec 31 '19 at 06:03
  • @BehradRanjbar Wow, thanks! – Furkan Yurdakul Dec 31 '19 at 06:37
  • messenger app, xRecorder app already have a solution... need to exist a solution, but i dont know yet :c – Alejandro Ibague May 09 '22 at 05:22

1 Answers1

1

Goto Settings->Permissions->AutoStart and then select your app

Muhammad Ali
  • 21
  • 1
  • 1
  • This does not remove the message that is printed as "Scheduling the restart of the crashed service: com.example.myapp/.MyService in 1000ms" so it only restarts the service, does not keep it. What I need is to keep the service in foreground, without the system crashing it, like other devices do. – Furkan Yurdakul Nov 19 '19 at 08:02
  • Only after Settings->Permissions->AutoStart enabled, the service was kept alive even after app was killed. – iCantC May 06 '21 at 05:12