Stanley Ko
Stanley Ko

Reputation: 3497

How to check the app was installed by apk or not

I need to check the app was installed by apk or from google play.

I have an apk that was downloaded from google play, and also, the app was available on google play. I want to classify two cases.

I already read about this question below, but it is about which app store the application was installed by, And it was not I want.
How does android package manager know what to install from the market place web site?

Is there any method to check the app was installed by apk or not, programmatically by app itself? Any help will be appreciated.

Upvotes: 4

Views: 3203

Answers (3)

Anuj Zunjarrao
Anuj Zunjarrao

Reputation: 426

First, find Installer package name, like:

String sourceId = getPackageManager()
    .getInstallerPackageName(getPackageName());

Then check the sourceId variable's value, like:

  • If App was installed from Google play store, said value should be:
com.android.vending
  • If installed from Google play store, then extracted APK, and installed again:
com.google.android.packageinstaller
  • If download from Google play store publish, App release list:
null

Upvotes: 6

Sabyasachi
Sabyasachi

Reputation: 3599

I use below code to check, if a apk was downloaded from a store or sideloaded:

private boolean isStoreVersion(Context context) {
    boolean isStoreVersion= false;

    try {
        String installer = context.getPackageManager()
                                    .getInstallerPackageName(context.getPackageName());
        isStoreVersion= !TextUtils.isEmpty(installer);
    } catch (Throwable e) {      
           e.printStackTrace();    
    }

    return isStoreVersion;

}

Upvotes: 0

Android_K.Doe
Android_K.Doe

Reputation: 753

Try this one

PackageManager pm = getPackageManager();

if(pm.getInstallerPackageName(getPackageName()).equals("com.android.vending"){
   //App is from google play
   //do something
}

app with unknown source will return empty string

Upvotes: 2

Related Questions