Install Referrer Kotlin Extension
source link: https://handstandsam.com/2022/02/28/install-referrer-kotlin-extension/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Sam Edwards in Updates | February 28, 2022
Install Referrer Kotlin Extension
There isn’t a Kotlin Extension (KTX) library for Google Play’s InstallReferrer Library, so I wanted to share how I wrapped the API in a very Kotlin friendly way.
The Install Referrer API allows you to securely retrieve referral content from Google Play. This can help you understand how you are acquiring new users in your app, and help you provide customized experiences tailored to where a user came from. For instance, if someone downloaded the app on a “New York” section of your website, you could open the “New York” section of the app on first app launch.
I hope this wrapper helps you!
import android.content.Context import android.os.RemoteException import com.android.installreferrer.api.InstallReferrerClient import com.android.installreferrer.api.InstallReferrerStateListener import com.android.installreferrer.api.ReferrerDetails import kotlinx.coroutines.CompletableDeferred
/** * https://developer.android.com/google/play/installreferrer/library * * implementation "com.android.installreferrer:installreferrer:2.2" */ object InstallReferrerExt { /** * Wraps callbacks to provide a nice Kotlin coroutine friendly interface. */ suspend fun getReferrerDetails(context: Context): ReferrerDetails? { val deferredReferrerDetails = CompletableDeferred<ReferrerDetails?>() val client = InstallReferrerClient.newBuilder(context.applicationContext).build() client.startConnection(object : InstallReferrerStateListener { override fun onInstallReferrerSetupFinished(responseInt: Int) { if (responseInt == InstallReferrerClient.InstallReferrerResponse.OK) { deferredReferrerDetails.complete( try { client.installReferrer } catch (e: RemoteException) { null } ) } else { deferredReferrerDetails.complete(null) } client.endConnection() }
override fun onInstallReferrerServiceDisconnected() { if (!deferredReferrerDetails.isCompleted) { deferredReferrerDetails.complete(null) } } }) return deferredReferrerDetails.await() } }
https://gist.github.com/handstandsam/686a1bb551d0426b51dd612890f64986
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK