【问题标题】:Android: check somewhere an app is installed via store or manually?Android:检查某个应用程序是通过商店还是手动安装的?
【发布时间】:2015-05-19 19:12:37
【问题描述】:

主题标题描述了我的问题。在 Android 上,我可以通过 Google Play 商店或手动检查/验证某处/以某种方式安装应用程序吗?手动是指从网上下载并安装,从 sd 卡等安装。

当您在网络上使用相同帐户访问 google play 时,Google play 也可以查看是否安装了应用程序。所以它在某处注册。例如,如果应用是通过 google play 安装的,是否可以“询问”google play?


编辑:根据 Marcin Orlowski 的回答,另请参阅下面的解决方案。

【问题讨论】:

  • 没有。因为这是在apk里面,还是不是?
  • @Morrison,你是对的!对此感到抱歉。虽然问题的标题有些复杂和不同。

标签: android installation google-play verify


【解决方案1】:

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

【讨论】:

  • 啊,很好。我会看看它。快速回答的坦克。
  • 问题:我需要知道 AndroidManifest.xml 文件中指定的包名称。如何在运行时获取包名?
  • 好吧,我需要指定包名才能使用函数getInstallerPackageName()。这就是我问的原因。但是已经搜索过了,是 .getPackageName() 来获取正在运行的应用程序的包名。
  • 啊,好的 - 是的,这是你的包 ID,getPackageName() 是在运行时调用的正确方法
  • 嗨,Marcin Orlowski,根据您的回答制定了解决方案。另请参阅下面的代码(第二个答案)
【解决方案2】:

好的,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
}

希望它对某人有所帮助。

【讨论】:

  • 由于 Play 商店不是唯一的现有 Android 商店,您可能还想检测以下其他商店:com.amazon.venezia - 亚马逊应用商店和 com.sec.android.app.samsungapps - 三星应用商店
  • 是的,我知道。这就是为什么该函数被称为“isAppPlayStoreGenuine()”而不是“isAppGenuineInstalled()”或类似的原因。我目前只使用 playstore/google play。无论如何感谢您的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 2013-07-11
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多