Android stop foreground service executes onStartCommand

1.2k views Asked by At

Why calling context.stopService(stopIntent) is going to execute onStartCommand am i missing something? i had to do this in the onStartCommand

if (ACTION_STOP_SERVICE == intent?.action) { stopSelf() }

While the doc said specifically this:

onStartCommand Called by the system every time a client explicitly starts the service by calling Context.startService

The code sample

@AndroidEntryPoint
class MyService : Service() {

    private val ACTION_STOP_SERVICE: String = "ACTION_STOP_SERVICE"
    private val ACTION_START_SERVICE: String = "ACTION_START_SERVICE"

    @OptIn(InternalCoroutinesApi::class)
    @ExperimentalCoroutinesApi
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        if (ACTION_START_SERVICE == intent?.action) {
            // Start mqtt client "org.eclipse.paho:org.eclipse.paho.client"
        }

        if (ACTION_STOP_SERVICE == intent?.action) {
            mqtt.disconnect()
            stopSelf()
        }        
        val stopIntent = Intent(this.applicationContext, BPSMqttService::class.java)
        stopIntent.action = ACTION_STOP_SERVICE

        val pStopIntent =
            PendingIntent.getService(
                BPSApp.instance.applicationContext,
                0,
                stopIntent,
                PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
            )
        val notificationIntent =
            Intent(this.applicationContext, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(
            this.applicationContext,
            0, notificationIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )

        val notification =
            NotificationCompat.Builder(this.applicationContext, SERVICE_CHANNEL_ID)
                .setContentTitle("My service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_alarms_sync)
                .setContentIntent(pendingIntent)
                .addAction(0, "Stop", pStopIntent)
                .build()
        startForeground(1, notification)
        return START_STICKY
    }

    override fun onDestroy() {
        try {
            super.onDestroy()
            Log.d("MyService ", "onDestroy is done")
        } catch (ex: Throwable) {
            Log.d("MyService ", "onDestroy ${ex.message}")
        }
    }

    override fun onCreate() {
        try {
            super.onCreate()
            Log.d("MyService ", "nCreate is done")
        } catch (ex: Throwable) {
            Log.d("MyService ", "onCreate ${ex.message}")
        }
    }
    
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }
}

And in other places other than the notification i am doing this to stop

val stopIntent = Intent(BPSApp.instance.applicationContext, BPSMqttService::class.java)
stopIntent.action = "ACTION_STOP_SERVICE"
BPSApp.instance.applicationContext.stopService(stopIntent)

And doing this to start

val startIntent = Intent(BPSApp.instance.applicationContext, BPSMqttService::class.java)
startIntent.action = "ACTION_START_SERVICE"
BPSApp.instance.applicationContext.startForegroundService(startIntent);
1

There are 1 answers

0
David Wasser On BEST ANSWER

It looks like you are seeing onStartCommand() being called as a result of your Notification.

The notification action "stop" will call startService(), so that's what you are seeing. In the other places in the code, when you call stopService(), onStartCommand() will not be called.