Last active
November 29, 2019 14:42
-
-
Save kibotu/0deabe671ae6de6ba970e3e08c9dc508 to your computer and use it in GitHub Desktop.
openAppOrMarket()
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
/** | |
* 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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
e.g.
"net.kibotu.schlachtensee".openAppOrMarket()