Viewed quite a few similar questions so far, but none ended up a solution.
I'm trying to start up a foreground service in order to handle SMS messaging. This service is started when the app is first opened and does work on background threads via kotlin coroutines. This process remains active even when the app has been closed.
I'm not too well versed on the various Contexts, but I would have thought applicationContext would exist for the lifetime of the app, but the call I make to get the SmsManager returns null and is therefore useless, regardless of whether the app is open.
Call to service in main activity:
val textServiceIntent = Intent(this@MainActivity, TextManager::class.java)
applicationContext.startForegroundService(textServiceIntent)
Call to getSystemService, returning null:
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val sms = applicationContext.getSystemService(SmsManager::class.java)
}
Note: I did also try putting this inside onCreate() where super.onCreate() is called, same issue.
Is there some other way of getting the context to access the SmsManager or any other system service inside of a service process?
Would passing a context from the main activity where it is first called be wise since the service will outlast the app being open?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
applicationContext.getSystemService<SmsManager>(SmsManager::class.java)
} else {
SmsManager.getDefault()
}