Skip to content

Instantly share code, notes, and snippets.

@imandaliya
Last active February 10, 2022 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imandaliya/944807b5acaf805e462ad7ca65e77279 to your computer and use it in GitHub Desktop.
Save imandaliya/944807b5acaf805e462ad7ca65e77279 to your computer and use it in GitHub Desktop.
// packageName = your application package name
fun Context.openInPlayStore() {
val playStoreApp = String.format("market://details?id=%", packageName)
val playStoreUrl = String.format("https://play.google.com/store/apps/details?id=%", packageName)
val playStoragePackage = "com.android.vending"
try {
if (appInstalledOrNot(playStoragePackage)) {
packageManager.getLaunchIntentForPackage(playStoragePackage)?.let { intent ->
intent.action = Intent.ACTION_VIEW
intent.data = Uri.parse(playStoreApp)
startActivity(intent)
} ?: run {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(playStoreApp)))
}
} else {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(playStoreApp)))
}
} catch (ex: ActivityNotFoundException) {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(playStoreUrl)))
} catch (ex: ActivityNotFoundException) {
Toast.makeText(this, "Couldn't find PlayStore on this device", Toast.LENGTH_SHORT).show()
}
}
}
fun Context.appInstalledOrNot(packageName: String): Boolean {
return try {
packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment