I'm running a service from MainActivity by
startService(mServiceIntent)
But there is only onCreate method invoked.I need to collect some extras arguments from Intent so I want to onStartCommand to be invoked too. Do You know what is the reason it's not happening. My Service code.
class ImportantService : Service() {
var phoneListener : MyPhoneStateListener? = null
var listening = false
var telephony: TelephonyManager? = null
override fun onBind(intent: Intent): IBinder {
TODO("Return the communication channel to the service.")
}
override fun onCreate() {
super.onCreate()
Log.d("ImportantService", "On Create")
//startListen()
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
val builder = NotificationCompat.Builder(this, "ch1")
.setContentText("Content text")
.setContentTitle("Content title")
startForeground(101,builder.build())
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId)
// this method is never invoked
Log.d("ImportantService", "On Start")
if(listening)
{
Log.d("ImportantService", "Started")
startListen()
}
else
{
Log.d("ImportantService", "Stopped")
// stopListen()
}
}
fun startListen()
{
Log.d("Service", "startListen")
}
}
Make sure to move return super.onStartCommand(intent, flags, startId)
to the end of your function.