Sometimes we have to check about the specific Application, Does the user has Installed in the phone in Android? In the following article, I am going to share a simple function that you can use to check if the Application is installed in Android Phone in Kotlin. There is also another function below which checks the enable status of the App. This function takes the Package name of the App you want to check its installation and returns true or false based on its installation status.
Function to Check If Application is Installed in Kotlin
fun isAppInstalled(packageName: String, context: Context): Boolean {
return try {
val packageManager = context.packageManager
packageManager.getPackageInfo(packageName, 0)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}
This function takes the Package name and Context as a parameter and returns a boolean. It tries to get the package info through the PackageManager, if it gets, it returns True. If a crash happens in the case of App not installed it returns False.
Function to Check Enable Status of App in Kotlin
In Android, users also have an option to disable some Apps, so in that case, the above method will not work. We also need to check App Enable status, for that you can use the following function.
fun isAppEnabled(packageName: String, context: Context): Boolean {
try {
val packageManager = context.packageManager
return packageManager.getApplicationInfo(packageName, 0).enabled
} catch (e: PackageManager.NameNotFoundException) {
return false
}
}
So this function will return true if the App is installed and is also enabled, otherwise, it will return false. PackageManager.getApplicationInfo method gives us ApplicationInfo object. This ApplicationInfo object has a boolean “enabled”. Same as in above method, we put try-catch here. If it couldn’t find the App it will raise NameNotFoundException and jump in to catch and will return false.
That’s it. This is how to check If Application is installed in Android Kotlin.
If you have any questions feel free to ask in the comments section below.