Android Kotlin get result of implicit intent RingtoneManager and use it later

200 Views Asked by At

It´s my first app with implicit intent and i´m trying to work with it with my books and android referenz for kotlin.

Now i got a problem on working with the result of an intent.

  1. i created a button
  2. OnClick at the button it starts "startActivityForResult" and opens the intent of the RingtoneManager
  3. Now i can choose the sound.

Everything is fine to this step. Now i read, that i have to use the "onActivityResult"-function to get the uri of the sound and then can use the uri to start the ringtone.

I just dont know how to get the this uri. "OnActiviyResult" gives a parameter with data. The data is an intent. I think i have to get the uri out of the data of the intent, but i dont know really how. Could someone help me pls, or just give me a hint? I don´t need the full solution. A hint would really help me.

    // starts on Using the Intent
    fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        Log.i("onActivityResult", "Start")
        val ringtoneTaken : Boolean = false

        super.onActivityResult(requestCode, resultCode, data)
        //val uri? : URI =
        //val uri = data.toUri()
        if(requestCode == 999 && resultCode == RESULT_OK) {
            if(data != null) {

                }
            }
            //saveSelectedRingtone()
        }
    }
1

There are 1 best solutions below

0
Da_Pusti On

Ok i finally found the answer now. It´s working with :

val uri = data!!.getParcelableExtra<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)

then i can set the new ringtone with

ringtone = RingtoneManager.getRingtone(this, uri)

and start playing it.