Detect If an App Is Installed from Play Store

Detect if an app is installed from Play store

This method will check if your app has been installed from the Play Store.

boolean verifyInstallerId(Context context) {
// A list with valid installers package name
List<String> validInstallers = new ArrayList<>(Arrays.asList("com.android.vending", "com.google.android.feedback"));

// The package name of the app that has installed your app
final String installer = context.getPackageManager().getInstallerPackageName(context.getPackageName());

// true if your app has been downloaded from Play Store
return installer != null && validInstallers.contains(installer);
}

Some days ago I released an Android library, PiracyChecker, that protects your app using some techniques, such as Google Play Licensing (LVL), APK signature protection and installer ID (this one).

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.

How to know an application is installed from google play or side-load?

The PackageManager class supplies the getInstallerPackageName method that will tell you the package name of whatever installed the package you specify. Side-loaded apps will not contain a value.

EDIT: Note @mttmllns' answer below regarding the Amazon app store.

Flutter how check if an application is installed from Google Play?

There is currently a package developed that checks exactly what you want.

How to detect if a specific app is being downloaded/installed by the Google Play store?

Is there any way to detect if the user is in the process of installing
app X?

The closest you'll get to this is by using a NotificationListenerService.

From the docs:

A service that receives calls from the system when new notifications
are posted or removed.

But considering this Service was recently added in API level 18 and your use case, you might consider using a BroadcastReceiver and listening for when android.intent.action.PACKAGE_ADDED and android.intent.action.PACKAGE_REMOVED are called. This way once an app is installed or removed, you can do what you want with the package name, like provide a link to the Play Store.

Here's an example:

BroadcastReceiver

public class PackageChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent == null || intent.getData() == null) {
return;
}

final String packageName = intent.getData().getSchemeSpecificPart();
// Do something with the package name
}

}

In your AndroidManifest

<receiver android:name="your_path_to.PackageChangeReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />

<data android:scheme="package" />
</intent-filter>
</receiver>


Related Topics



Leave a reply



Submit