Check If Application Is Installed - Android

Check if application is installed - Android

Try this:

private boolean isPackageInstalled(String packageName, PackageManager packageManager) {
try {
packageManager.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

It attempts to fetch information about the package whose name you passed in. Failing that, if a NameNotFoundException was thrown, it means that no package with that name is installed, so we return false.

Note that we pass in a PackageManager instead of a Context, so that the method is slightly more flexibly usable and doesn't violate the law of Demeter. You can use the method without access to a Context instance, as long as you have a PackageManager instance.

Use it like this:

public void someMethod() {
// ...

PackageManager pm = context.getPackageManager();
boolean isInstalled = isPackageInstalled("com.somepackage.name", pm);

// ...
}

Note: From Android 11 (API 30), you might need to declare <queries> in your manifest, depending on what package you're looking for. Check out the docs for more info.

How to check programmatically if an application is installed or not in Android?

Try with this:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Add respective layout
setContentView(R.layout.main_activity);

// Use package name which we want to check
boolean isAppInstalled = appInstalledOrNot("com.check.application");

if(isAppInstalled) {
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.check.application");
startActivity(LaunchIntent);

Log.i("SampleLog", "Application is already installed.");
} else {
// Do whatever we want to do if application not installed
// For example, Redirect to play store

Log.i("SampleLog", "Application is not currently installed.");
}
}

private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}

return false;
}

}

How to know if a particular application is installed or not on the device?

I found the answer here

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList =
context.getPackageManager().queryIntentActivities( mainIntent, 0);
for (ResolveInfo resolveInfo : pkgAppsList) {
Log.d(TAG, "__<>"+resolveInfo.activityInfo.packageName);
if (resolveInfo.activityInfo.packageName != null
&& resolveInfo.activityInfo.packageName.equals(uri)) {
return true;
}
}
return false;

This worked for me perfectly.

Detect an application is installed or not?

From Android How-to's

If you ever need to know if a particular app is installed on the user's device, you can use the PackageManager. From a Context class (e.g. an Activity or a Service) you can call getPackageManager(). This gives you a variety of methods, one of which is getPackageInfo(). Below is a method you might use. You might call it like this:

isAppInstalled("com.simexusa.campusmaps_full");

private boolean isAppInstalled(String packageName) {
PackageManager pm = getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}

Check if an application is installed from Google Play

This should work for that purpose, taken from HockeyAppSDK for Android Github library:

protected static boolean installedFromMarket(WeakReference<? extends Context> weakContext) {
boolean result = false;

Context context = weakContext.get();
if (context != null) {
try {
String installer = context.getPackageManager().getInstallerPackageName(context.getPackageName());
// if installer string is not null it might be installed by market
if (!TextUtils.isEmpty(installer)) {
result = true;

// on Android Nougat and up when installing an app through the package installer (which HockeyApp uses itself), the installer will be
// "com.google.android.packageinstaller" or "com.android.packageinstaller" which is also not to be considered as a market installation
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (TextUtils.equals(installer, INSTALLER_PACKAGE_INSTALLER_NOUGAT) || TextUtils.equals(installer, INSTALLER_PACKAGE_INSTALLER_NOUGAT2))) {
result = false;
}

// on some devices (Xiaomi) the installer identifier will be "adb", which is not to be considered as a market installation
if (TextUtils.equals(installer, INSTALLER_ADB)) {
result = false;
}
}

} catch (Throwable ignored) {
}
}

return result;
}

However, I'd suggest to build two different apps for that purpose. If you're including forbidden code in Google Play, although it won't be executed helped by this method, it will be part of your bytecode and it's very likely to be banned by Google. It's much more cleaner to strip that out of your code by creating another flavour.



Related Topics



Leave a reply



Submit