Android App in Kotlin - Requesting user to set app as default SMS app


Being new to Android App programming and Kotlin I’m experimenting with requesting the user to make my app the default SMS app. So far I have managed for my test app to be among the choices for the default SMS app, but I want to request the user’s consent to make my app the default SMS app, performing my action and to switch back to the previous default SMS app.

This is the code I have come up with

val defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(getApplicationContext())

val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getApplicationContext().getPackageName())

and after the action of my app is peformed to switch back to the original default SMS App

val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp);

However, having tested the code in a virtual device the system does not ask the user to make the app the default SMS App, but starts it right away. In addition the “defaultSMSApp” variable is null. What do I miss here? Can anybody help?

Thanks for your support