My app uses a Service for background tasks. I want the Service to keep running when user kills the app(swipes it away). There are two scenario's in which the user kan kill the app:
Scenario 1: When in app:
1 User presses **backbutton**, apps goes to background and user is on the homescreen.
2 User presses the multitasking button and swips the app away.
3 The Service should restart because its sticky.
Scenario 2: When in app:
1 User presses **homebutton**, apps goes to background and user is on the homescreen.
2 User presses the multitasking button and swips the app away.
3 The Service should restart because its sticky.
Scenario 1 works exactly as expected. The Service restarts after the app is swiped away in a matter of seconds.
Scenario 2 does not work as expected. The service does get restarted but after more than 5 minutes.
I don't understand why it takes so long to restart the service in scenario 2. Is there a known solution to this?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent test = new Intent(this, TestService.class);
startService(test);
}
}
public class TestService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("Service", "Service is restarted!");//In Scenario 2, it takes more than 5 minutes to print this.
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}