user3782869
user3782869

Reputation: 3

how to keep the service running in background even after the app is closed/killed?

there are many articles written on the topic I specified. However, all are seems to be not working. At least in my case. I just want to run my service all the time even after the app is close manually by user. I don't want foreground service as it display some notification icon in the status bar. I want regular service to complete my requirement. Or please suggest if any other alternative available to achieve this. I tried with return START_STICKY as return value for onStartCommand call back. I also tried with broadcast receiver which doesn't help me anyway. Please help me on this.

Upvotes: 0

Views: 872

Answers (2)

K Sathish
K Sathish

Reputation: 257

You can create the alarm with 5 mins in the onTaskRemoved() method in your service class. It will automatically invoke after the 5mins and restarts the service.

In Service Class

public void onTaskRemoved(Intent rootIntent) {
     Intent restartService = new Intent(getApplicationContext(), YourService.class);
     restartService.setPackage(Yourpackagename);
     PendingIntent restartServiceIntent = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
     AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
     alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealTime()+1000, restartServiceIntent);
}

Upvotes: 1

Kommineni Veeranayana
Kommineni Veeranayana

Reputation: 103

You can achieve this functionality by implementing the AlarmManger concept in Android. Take a look at following URLs once you will get idea to implement this background service.

Hint: You should make sure about this before implementing. Because it will consume battery a lot.

Good luck

http://stacktips.com/tutorials/android/repeat-alarm-example-in-android

https://developer.android.com/reference/android/app/AlarmManager.html

Upvotes: 0

Related Questions