I want to do deferred deep-link from my app to an external app (app I am not in control of) on android. It works perfectly to both open the app in playstore if it is not installed on the device, and it works if the app is indeed installed and open an intent at a specific location in the app. I am struggling with doing the installation of the app and then when user opens the app, redirecting to the location specified by the Intent.
The way I'm doing it now is checking whether the app is installed or not, and if it is installed I open it with the intent that opens the app in the desired location. These 2 intents acts as I anticipate: first opens the play store and installs the app, the other opens the app at a specific location.
fun start() {
if (isAppInstalled()) {
startAppWithIntent(deepLink)
} else {
goToPlayStore()
}
}
private fun isAppInstalled(): Boolean =
activity.packageManager.getLaunchIntentForPackage("com.myapp.xx") != null
private fun startAppWithIntent(deepLink: String) {
val startAssetIntent = Intent(Intent.ACTION_VIEW, Uri.parse(deepLink))
activity.startActivity(startAssetIntent)
}
private fun goToPlayStore() {
val goToPlayStoreIntent = Intent(
Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.myapp.xx")
).apply {
setPackage("com.android.vending")
}
activity.startActivity(goToPlayStoreIntent)
activity.finish()
}
The problem with this however is that when the app is not installed and user installs and opens the app, it opens at the homepage of the app.
How can I construct an Intent that first installs the app and then proceeds to open the app at the desired location in one go? Is it even possible? Or is this the responsibility of the installed app?
It is correct what @pantos27 is saying. It seems like the receiving app needs to have a BroadcastReceiver()
which can listen to an event
on when the app is installed.
See Official Docs for more details