Home > Back-end >  Is using a Service onStartCommand for multiple intents bad practice?
Is using a Service onStartCommand for multiple intents bad practice?

Time:07-15

I have a long-running foreground service which generates high priority notifications that require a yes/no button press in order to be dismissed.

The button presses are handled by addAction calls, where I must send in an Intent that will transmit the resulting button press.

Right now, I'm sending everything back to the foreground service, and the Intents are being picked up by the onStartCommand, like this:

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

    if (intent.action.equals("START_SERVICE")) {
        ...
    }

    if (intent.action.equals("STOP_SERVICE")) {
        ...
    }

    // etc....

}

My question is - is this ok? I know I can also set up a separate BroadcastReceiver to receive the Intents and communicate forward from there, but since I need to end up calling functions within the foreground service context, it seems like a bad idea to do all that. I wish the onStartCommand was named onReceive, I would feel better!

CodePudding user response:

This is fairly standard for a non-bound service. If the service is bound you'd generally use the binder instead, but that's a bit awkward via a notification.

  • Related