We have installed applications programmatically.
So nicer with Kotlin suger:
private fun isSomePackageInstalled(context: Context, packageName: String): Boolean {
val packageManager = context.packageManager
return runCatching { packageManager.getPackageInfo(packageName, 0) }.isSuccess
}
Check App is installed or not in Android by using kotlin.
Creating kotlin extension.
fun PackageManager.isAppInstalled(packageName: String): Boolean = try {
getApplicationInfo(packageName, PackageManager.GET_META_DATA)
true
} catch (e: Exception) {
false
}
Now, can check if app is install or not
if (packageManager.isAppInstalled("AppPackageName")) {
// App is installed
}else{
// App is not installed
}
This code is used to check weather your application with package name is installed or not if not then it will open playstore link of your app otherwise your installed app
String your_apppackagename="com.app.testing";
PackageManager packageManager = getPackageManager();
ApplicationInfo applicationInfo = null;
try {
applicationInfo = packageManager.getApplicationInfo(your_apppackagename, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (applicationInfo == null) {
// not installed it will open your app directly on playstore
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + your_apppackagename)));
} else {
// Installed
Intent LaunchIntent = packageManager.getLaunchIntentForPackage(your_apppackagename);
startActivity( LaunchIntent );
}
The above code didn't work for me. The following approach worked.
Create an Intent object with appropriate info and then check if the Intent is callable or not using the following function:
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
I think using try/catch pattern is not very well for performance. I advice to use this:
public static boolean appInstalledOrNot(Context context, String uri) {
PackageManager pm = context.getPackageManager();
List<PackageInfo> packageInfoList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);
if (packageInfoList != null) {
for (PackageInfo packageInfo : packageInfoList) {
String packageName = packageInfo.packageName;
if (packageName != null && packageName.equals(uri)) {
return true;
}
}
}
return false;
}
All the answers only check certain app is installed or not. But, as we all know an app can be installed but disabled by the user, unusable.
Therefore, this solution checks for both. i.e, installed AND enabled apps.
public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
try {
return packageManager.getApplicationInfo(packageName, 0).enabled;
}
catch (PackageManager.NameNotFoundException e) {
return false;
}
}
Call the method isPackageInstalled()
:
boolean isAppInstalled = isPackageInstalled("com.android.app" , this.getPackageManager());
Now, use the boolean variable isAppInstalled
and do whatever you want.
if(isAppInstalled ) {
/* do whatever you want */
}