Android:检查某个应用程序是通过商店还是手动安装的?
Android: check somewhere an app is installed via store or manually?
标题描述了我的问题。在 Android,我可以 check/verify somewhere/somehow 通过 google Play 商店或手动安装应用程序吗?手动我的意思是它从网上下载并安装,从 sd-card 等安装
Google play 可以在您访问 google 在具有相同帐户的另一台设备上在网络上播放时查看是否安装了应用程序。所以它在某个地方注册了。例如,如果应用程序是通过 google 播放安装的,是否可以 'ask' google 播放?
编辑:根据 Marcin Orlowski 的回答,另请参阅下面我的解决方案。
包管理器中有 getInstallerPackageName() 方法。对于旁加载的 APK,它将 return 没有名称。
好吧,Marcin Orlowski 得到了正确的答案并用它设计了这个。问题是,当您在 运行 来自 IDE (Android Studio) 的应用程序时使用此检测方法时,该功能检测到它不是通过 Google Play 安装的。为避免这种行为,您必须将构建类型 'debug' 添加到 build.gradle 文件中。例如:
debug {
applicationIdSuffix ".debug"
versionNameSuffix ".debug"
}
代码:
........
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) );
}
您可以检查当前应用是否正确安装,也可以指定其他应用的appId。当没有安装 Google Play 时,永远无法通过 Google Play.
正确安装
例如:
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
}
希望对某人有所帮助。
标题描述了我的问题。在 Android,我可以 check/verify somewhere/somehow 通过 google Play 商店或手动安装应用程序吗?手动我的意思是它从网上下载并安装,从 sd-card 等安装
Google play 可以在您访问 google 在具有相同帐户的另一台设备上在网络上播放时查看是否安装了应用程序。所以它在某个地方注册了。例如,如果应用程序是通过 google 播放安装的,是否可以 'ask' google 播放?
编辑:根据 Marcin Orlowski 的回答,另请参阅下面我的解决方案。
包管理器中有 getInstallerPackageName() 方法。对于旁加载的 APK,它将 return 没有名称。
好吧,Marcin Orlowski 得到了正确的答案并用它设计了这个。问题是,当您在 运行 来自 IDE (Android Studio) 的应用程序时使用此检测方法时,该功能检测到它不是通过 Google Play 安装的。为避免这种行为,您必须将构建类型 'debug' 添加到 build.gradle 文件中。例如:
debug {
applicationIdSuffix ".debug"
versionNameSuffix ".debug"
}
代码:
........
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) );
}
您可以检查当前应用是否正确安装,也可以指定其他应用的appId。当没有安装 Google Play 时,永远无法通过 Google Play.
正确安装例如:
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
}
希望对某人有所帮助。