I am using the ringtone picker provided by the Android RingtoneManager. When I choose ringtone and press OK, resultCode is Activity.RESULT_OK and data contains the ringtone's URI. When I press Cancel resultCode is Activity.RESULT_CANCELED. So far, so good, this works as expected. But when I choose Silent or None and press OK, resultCode is also RESULT_CANCELED, so I can't tell, whether the user has just canceled the picker dialog or really does not want a ringtone to be played.
What is going wrong here?
const val PICK_TONE = 1
btnRtp.setOnClickListener {
intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER)
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION)
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Choose ringtone")
startActivityForResult(intent, PICK_TONE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_TONE) {
if (resultCode == Activity.RESULT_CANCELED) {
textView.text = "Canceled"
} else
if (resultCode == Activity.RESULT_OK) {
textView.text = ""
var toneUri: Uri? =
data?.getParcelableExtra<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
textView.text = toneUri?.toString() ?: "Silent"
}
}
}
I figured out the solution myself. Turns out, that if you do not pass a URI via intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentTone)
Silent is preselected by the picker but clicking OK does not give RESULT_OK. When I pass a valid URI to the picker the picker returns RESULT_OK when clicking OK no matter what tone was selected. Docs could be a little more precise here...