Android:检查通过商店或手动安装应用程序的某个地方?

时间:2015-05-19 19:12:37

标签: android installation google-play verify

主题标题描述了我的问题。在Android上,我可以通过谷歌游戏商店或手动检查/验证某个地方/以某种方式安装应用程序?手动我的意思是它从网上下载并安装,从SD卡等安装。

当您在具有相同帐户的其他设备上访问网络上的Google Play时,Google Play也可以查看是否安装了应用。所以它已在某处注册。例如,如果通过谷歌播放安装应用程序,是否可以“询问”谷歌播放?

编辑:根据Marcin Orlowski的答案,参见下面的解决方案。

2 个答案:

答案 0 :(得分:1)

PackageManager中有getInstallerPackageName()方法。对于侧载APK,它将不返回任何名称。

答案 1 :(得分:0)

好吧,Marcin Orlowski得到了正确的答案并设计了它。问题是,当您从IDE(Android Studio)运行应用程序时使用此检测方法时,该功能会检测到它未通过Google Play安装。要避免此行为,必须将buildtype“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
}

希望能帮助别人。