Home » Android » Running service after application close

Running service after application close

Posted by: admin November 1, 2017 Leave a comment

Questions:

My application need to run service in background even if user closed the app.
But, when I closed my app, the service stop and restart. So, all variables who are in the service are reset.

After search on google, I have found that :

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

But it do not work. My service is reset.

Answers:

Hello you need to consider the fact that If your app targets API level 26 or higher, the system imposes restrictions on running background services when the app itself is not in the foreground. In most cases like this, your app should use a scheduled job instead. You can read more on this link:
Services

Questions:
Answers:

Did you add your service to the AndroidManifest.xml?