Search code examples
androidkotlinreferrerurl

How to get variables out from a asynctask + try & catch block in kotlin (android studio)?


In these below two functions I am getting referrerUrl and addId. I want both of them to be fetched in onCreate but don't know how because it is in try & catch block also the getGaid() function is not running without AsyncTask.

    fun getreferrUrl() {

     //to install referrer client
   val referrerClient = InstallReferrerClient.newBuilder(this).build()
    referrerClient.startConnection(object : InstallReferrerStateListener {

        override fun onInstallReferrerSetupFinished(responseCode: Int) {
            when (responseCode) {
                InstallReferrerResponse.OK -> {
                    // Connection established.
                    try {
                        val response: ReferrerDetails = referrerClient.installReferrer
                        val referrerUrl = response.installReferrer

                                // here we need referrerUrl out from this fuction

                    } catch (e: RemoteException) {
                        e.printStackTrace()
                    }
                }

//

fun getGaid() {
    AsyncTask.execute {

        try {
            val adInfo = AdvertisingIdClient.getAdvertisingIdInfo(this)
            val myId: String = if (adInfo != null) adInfo.id else null.toString()

                       //here we need myId out from this fuction

        } catch (e: java.lang.Exception) {...}
     }  
  }

In onCreate we need both of those strings.

      // In onCreate

                 val url = "http://instbng.com?device_id=$device_id&                             
                            &kd_id=$kd_id&ref=$referrerUrl&gaid=$myId"

                   loadUrl(url)

Solution

  • Without coroutines, you can put the results in properties, and create a function that uses both properties and call it from both callbacks. I renamed your get... functions to fetch... since they are asynchronous. The word get in a function name implies they are synchronous.

    private var referrerUrl: String? = null
    private var myId: String? = null
    
    override fun onCreate(bundle: SavedInstanceState?) {
        super.onCreate(bundle)
    
        //...
    
        fetchReferrerUrl()
        fetchGaId()
    }
    
    // proceeds with workflow if referrerUrl and myId are both available
    private fun proceedIfReady() {
        val referrer = referrerUrl ?: return
        val id = myId ?: return
        val url = "http://instbng.com?device_id=$device_id&kd_id=$kd_id&ref=$referrer&gaid=$idd"
        loadUrl(url)
    }
    
    fun fetchReferrerUrl() {
        val referrerClient = InstallReferrerClient.newBuilder(this).build()
        referrerClient.startConnection(object : InstallReferrerStateListener {
    
            override fun onInstallReferrerSetupFinished(responseCode: Int) {
                when (responseCode) {
                    InstallReferrerResponse.OK -> {
                        // Connection established.
                        try {
                            val response: ReferrerDetails = referrerClient.installReferrer
                            referrerUrl = response.installReferrer
    
                            proceedIfReady()
    
                        } catch (e: RemoteException) {
                            e.printStackTrace()
                        }
                    }
                }
            }
    
            //... handle closed connection callback
    
        }
    }
    
    private fun fetchGaId() {
        AsyncTask.execute {
            try {
                val adInfo = AdvertisingIdClient.getAdvertisingIdInfo(this)
                runOnUiThread { // in a Fragment use view?.post
                    myId = if (adInfo != null) adInfo.id else null.toString()
                    proceedIfReady()
                }
            } catch (e: java.lang.Exception) {...}
        }
    }