First of all, you need to decide whether you want the user to know about your running service or not. Check out Background Execution Limitations in android Oreo :
To improve the user interface, Android 8.0 (API level 26) imposes restrictions on what applications can execute while running in the background.
So, given your case, when there seems to be a lot of work in many situations, it would be better to use the foreground service. As the Android doc says about front-end services :
A foreground service is a service that the user is actively aware of and is not a candidate for that system can kill when it is in memory. The foreground service should provide a notification for the status bar, which is placed under the "Current" heading. This means that the notification cannot be rejected unless the service is stopped or removed from the foreground.
Since you mentioned that you have detected an action, I will not enter this part of your code. Therefore, you need to subclass Service as you did, and use the startService method to call it onCreate . You should notice that the onCreate service onCreate is called as soon as you call startService in this service for the first time, no matter how many times you call startService again, the onCreate method onCreate not be called and only onStartCommand will be called. We use this fact along with the fact that you can provide a string action in your intent to correctly register and unregister your listener.
In MainActivity.java :
String action = "start"; // Or to unregister listener "stop"! final Intent intent = new Intent(this, MyService.class); intent.setAction(action); startService(intent);
and then in MyService.java :
@Override public void onCreate() { super.onCreate(); // Do initialization or whatever here (executed once per service lifecycle) } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent.getAction().equals("start")) { // Register your listener or whatever showForegroundNotification(); } if (intent.getAction().equals("stop")) { // Unregister your listener or whatever stopForeground(true); stopSelf(); } return START_STICKY; } private void showForegroundNotification() { Intent myServiceNotificationIntent = new Intent(this, MainActivity.class); myServiceNotificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent .getActivity(this, MY_SERVICE_REQUEST_CODE, myServiceNotificationIntent, MY_SERVICE_FLAG); Notification notification = new NotificationCompat.Builder(this) .setContentTitle(MY_SERVICE_NOTIFICATION_CONTENT_TITLE) .setTicker(MY_SERVICE_NOTIFICATION_TICKER) .setContentText(MY_SERVICE_NOTIFICATION_CONTENT_TEXT) .setSmallIcon(R.drawable.ic_whatever) .setContentIntent(pendingIntent) .setOngoing(true) .build(); startForeground(MY_SERVICE_NOTIFICATION_ID, notification); }
Finally, do not forget to unregister your listener in onDestroy if the android killed your service (which is very rare):
@Override public void onDestroy() { super.onDestroy();