[英]How can I know that my app was opened by Google Assistant, instead of just normally launched
我怎么知道我的应用是由 Google 助理打开的,而不是正常启动的。 我不需要应用操作。 我只是想知道,是的,我的应用程序是用“Ok Google -> Open appname”打开的,而不是按图标,或者从最近恢复它。 如果捆绑包中有意图/任何数据,我可以检查吗?
这是我执行“打开应用程序名称”时的意图
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.xelion.android cmp=com.xelion.android/.activity.InitializationActivity (has extras) }
它有额外的,但不知道是什么:
Bundle[mParcelledData.dataSize=220]
我发现这将是使用 google Assistant 打开的标志:
intent.flags == 0x10000000
但我的问题是,当我从机器构建应用程序或更新它时,这也会运行,知道如何避免这种情况吗?
我也试过:
private fun getReferrerCompatible(activity: Activity): Uri? {
val intent = activity.intent
val referrerUri: Uri? = intent.getParcelableExtra(Intent.EXTRA_REFERRER)
if (referrerUri != null) {
return referrerUri
}
val referrer = intent.getStringExtra(REFERRER_NAME)
if (referrer != null) {
// Try parsing the referrer URL; if it's invalid, return null
try {
return Uri.parse(referrer)
} catch (e: ParseException) {
return null
}
}
return null
}
但我仍然得到 NULL 作为推荐人
我正在尝试: intent.extras?.get(KEY_REF_NAME) == REG_G_ASSISTANT
或来自 onCreate 的getReferrerCompatible()
。 应该晚点吗? 喜欢 onResume?
通过 Google 助理打开时, android.intent.extra.REFERRER_NAME
将为android-app://com.google.android.googlequicksearchbox/https/www.google.com
val KEY_REF_NAME = "android.intent.extra.REFERRER_NAME"
val REG_G_ASSISTANT = "android-app://com.google.android.googlequicksearchbox/https/www.google.com"
if (intent.extras?.get(KEY_REF_NAME) == REG_G_ASSISTANT) {
// APP OPENED THROUGH GOOGLE ASSISTANT
} else {
// APP OPENED THROUGH DEFAULT LAUNCHER
}
基于theapache64给出的响应和这个链接: https://github.com/allegro/slinger/blob/master/slinger/src/main/java/pl/allegro/android/slinger/ReferrerMangler.Z93F725A07423FE1C889F448B33D21F46
因为意图是在 Android 10 上返回 null,并且由于我的最小 SDK 为 23(我不需要在 M 下实现逻辑),我已经完成了以下代码:
val REG_G_ASSISTANT = "com.google.android.googlequicksearchbox"
if (referrer != null && referrer.toString().contains(REG_G_ASSISTANT)) {
//code to do
}
这是 Kotlin,并且正在进行活动。 相当于 in.java 的推荐人将是:
activity.getReferrer();
如果您在 23 岁以下运行操作系统,则可以像这样使用引荐来源网址:
val KEY_REF_NAME = "android.intent.extra.REFERRER_NAME"
intent.extras?.get(KEY_REF_NAME)
由于 theapache64 在 OnePlus6 上尝试过,我认为这应该在某些设备上的 API 级别 28(Pie)之前有效。 但可以肯定的是,我建议使用activity.getReferrer()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.