Reputation: 6610
The subject title describes my question. On Android, can I check/verify somewhere/somehow an app is installed via google play store or manually? With manually I mean it's downloaded from the web and installed, installed from sd-card etc.
Google play is able to see if an app is installed also when you visit google play on the web, on another device with the same account. So it's registered somewhere. For example, is it possible to 'ask' google play if the app is installed via google play?
EDIT: See also my solution below based upon the answer of Marcin Orlowski.
Upvotes: 1
Views: 1668
Reputation: 6610
Allright, Marcin Orlowski got the right answer and designed this with it. The problem is, when you using this detection method when running the app from the IDE (Android Studio), the function detects that it is not installed via Google Play. To avoid this behaviour, you must add the buildtype 'debug' to the build.gradle file. for example:
debug {
applicationIdSuffix ".debug"
versionNameSuffix ".debug"
}
The code:
........
int playStoreInstalled = -1;
........
public boolean isDebugVersion()
{
// NOTICE: To make this functional, specify a debug buildType in the build.gradle file
try {
// Get the applicationId specified in the build.gradle file
String sAppId = this.mContext.getPackageName();
return sAppId.endsWith(".debug");
} catch( Exception e ) {}
return true;
}
public String getInstallerPackageName( String sPackageName )
{
String sInstallerName = "";
try
{
if( sPackageName == null || sPackageName.length() == 0 )
{ sPackageName = this.mContext.getPackageName(); }
PackageManager packageManager = this.activity.getApplication().getPackageManager();
sInstallerName = packageManager.getInstallerPackageName(sPackageName);
} catch( Exception e ) {}
return sInstallerName;
}
public boolean isPlayStoreInstalled()
{
// Check it only once, is the play store installed?
// NOTICE: At first check this.playStoreInstalled is initialized with -1
if( this.playStoreInstalled < 0 )
{
// Because playstore it's name has changed, we must check for both
String sPlayStorePackageNameOld = "com.google.market";
String sPlayStorePackageNameNew = "com.android.vending";
String sPackageName = "";
PackageManager packageManager = this.activity.getApplication().getPackageManager();
List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for( PackageInfo packageInfo : packages)
{
sPackageName = packageInfo.packageName;
//this.debug( sPackageName );
if(sPackageName.equals(sPlayStorePackageNameOld) || sPackageName.equals(sPlayStorePackageNameNew))
{
this.playStoreInstalled = 1;
break;
}
}
}
return ( this.playStoreInstalled > 0 );
}
public boolean isAppPlayStoreGenuine( String sPackageName )
{
// If there is no playstore available, it is impossible that the app
// is genuine installed via the playstore.
if( !this.isPlayStoreInstalled())
{ return false; }
String sInstallerName = this.getInstallerPackageName( sPackageName );
return (sInstallerName != null && sInstallerName.length() > 0 );
}
public boolean isAppPlayStoreGenuine() // Check current app is properly/official genuine installed
{
return ( this.isDebugVersion() || this.isAppPlayStoreGenuine(null) );
}
You can check if the current app is properly installed or you can specify an appId of another app. When there is no Google Play installed, it can never been properly installed via Google Play.
For example:
if( !this.isAppPlayStoreGenuine())
{
// 1. Show message before shutdown
// 2. Shutdown app
}
// or:
if( !this.isAppPlayStoreGenuine('com.mycompany.myapp'))
{
// 1. Show message
// 2. Do what you want to do
}
Hopes it helps someone.
Upvotes: 0
Reputation: 75629
There is getInstallerPackageName() method in PackageManager. For side-loaded APKs it will return no name.
Upvotes: 1