Last active
February 10, 2022 13:16
-
-
Save imandaliya/944807b5acaf805e462ad7ca65e77279 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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