安卓安装源是什么?

11

是否可以从Android应用程序中获取安装源代码?

我的意思是,我想看看该应用程序是从Play StoreAmazon Appstore还是通过执行APK安装的。

PS:我认为在我的应用程序崩溃并且我能够发送错误报告时,可以看到它是通过Play Store安装的。我怎么得到这个值呢?

4个回答

16

3

这是一个实用函数(使用Kotlin编写),您可以将其添加到代码中:

/**
 * Retrieves the name of the app responsible for the installation of this app.
 * This can help in identifying which market this app was installed from or whether the user
 * sideloaded it using an APK (Package Installer).
 */
fun getAppInstaller(context: Context): String {
    val appContext = context.applicationContext

    val installerPackageName = try {
        val appPackageManager = appContext.packageManager
        val appPackageName = appContext.packageName

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
            appPackageManager.getInstallSourceInfo(appPackageName).installingPackageName
        else
            appPackageManager.getInstallerPackageName(appPackageName)
    } catch (e: Exception) {
        e.printStackTrace()
        "--"
    }

    return when (installerPackageName) {
        "com.android.vending" -> "Google Play Store"
        "com.amazon.venezia" -> "Amazon AppStore"
        "com.huawei.appmarket" -> "Huawei AppGallery"
        "com.google.android.packageinstaller" -> "Package Installer"
        else -> installerPackageName ?: "Unknown"
    }
}

我使用了这个StackOverflow问题以及其他人的答案来创建上述函数。您可以调用此函数,将上下文传递给它,然后将返回值设置为Crashlytics中的自定义键,或记录为分析数据,或任何您想要的操作。


3

目前(2022年1月16日)的代码:

val s = applicationContext.getPackageManager().getInstallerPackageName(applicationContext.getPackageName())

在GooglePlay上,适用于Android 11,并获得“com.android.vending”。


2

使用getInstallerPackageName方法。

这将存储安装应用程序的包名。

  • Google Play: "com.android.vending"
  • Amazon Appstore: "com.amazon.venezia"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接