Skip to content

Instantly share code, notes, and snippets.

@kibotu
Last active November 29, 2019 14:42
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 kibotu/0deabe671ae6de6ba970e3e08c9dc508 to your computer and use it in GitHub Desktop.
Save kibotu/0deabe671ae6de6ba970e3e08c9dc508 to your computer and use it in GitHub Desktop.
openAppOrMarket()
/**
* Open another app.
*
* source: https://stackoverflow.com/a/7596063/1006741
*
* @param packageName the full package name of the app to open
*/
fun Activity.openMainLauncherApp(packageName: String?) = with(packageManager.getLaunchIntentForPackage(packageName!!)!!) {
addCategory(Intent.CATEGORY_LAUNCHER)
startActivity(this)
}
fun String.openMainLauncherApp() = currentActivity?.openMainLauncherApp(this)
fun String.openMarket() = currentActivity?.openMarket(this)
fun Activity.openMarket(packageName: String) = try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName")))
} catch (e: Exception) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=${packageName}")))
}
fun String.openAppOrMarket() = try {
openMainLauncherApp()
} catch (e: Exception) {
openMarket()
}
@kibotu
Copy link
Author

kibotu commented Nov 29, 2019

e.g. "net.kibotu.schlachtensee".openAppOrMarket()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment