Is the following statement correct for Android service - If we call startForeground() inside onStartCommand() or inside onCreate() of the service class then it is a foreground service and if we do not call then it becomes a background service. Here is the code - in this code I am calling the startForeground() method which takes a notification ID and object of a notification class. Now it is a foreground service and if we do not call this method then it becomes a background service. Is this the correct statement? The code is working fine.
class MusicPlayerService : Service() {
private companion object{
const val NOTIFICATION_ID = 1
}
override fun onCreate() {
super.onCreate()
startForeground(NOTIFICATION_ID, createNotification())
}
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//service code
return START_STICKY
}
private fun createNotification() : Notification{
//Code for creating a notification code
}
}