我正在使用 Android RingtoneManager 提供的铃声选择器。当我选择铃声并按 OK 时,resultCode 是 Activity.RESULT_OK 并且 data 包含铃声的 URI。当我按下取消时,resultCode 是 Activity.RESULT_CANCELED。到目前为止,一切都很好,这按预期工作。但是当我选择 Silent 或 None 并按 OK 时,resultCode 也是 RESULT_CANCELED,所以我无法判断用户是刚刚取消了选择器对话框还是真的不想播放铃声。
这里出了什么问题?
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"
}
}
}