[英]How can I know that my app was opened by Google Assistant, instead of just normally launched
How can I know that my app was opened by Google Assistant, instead of just normally launched.我怎么知道我的应用是由 Google 助理打开的,而不是正常启动的。 I don't need App Actions.
我不需要应用操作。 I just want to know, that yes, my app was opened with "Ok Google -> Open appname" instead of pressing on the icon, or resuming it from recents.
我只是想知道,是的,我的应用程序是用“Ok Google -> Open appname”打开的,而不是按图标,或者从最近恢复它。 If there an intent/any data in the bundle that I can check for that?
如果捆绑包中有意图/任何数据,我可以检查吗?
This is my intent when I do "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) }
And it has extras, but don't know what:它有额外的,但不知道是什么:
Bundle[mParcelledData.dataSize=220]
I found out that this will be the flag for opening with google Assistant:我发现这将是使用 google Assistant 打开的标志:
intent.flags == 0x10000000
But my problem is that this also will run when I build the app from machine or update it, Any idea how to avoid that?但我的问题是,当我从机器构建应用程序或更新它时,这也会运行,知道如何避免这种情况吗?
I have also tried:我也试过:
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
}
But I still get NULL as referrer但我仍然得到 NULL 作为推荐人
I am trying the: intent.extras?.get(KEY_REF_NAME) == REG_G_ASSISTANT
or getReferrerCompatible()
from the onCreate.我正在尝试:
intent.extras?.get(KEY_REF_NAME) == REG_G_ASSISTANT
或来自 onCreate 的getReferrerCompatible()
。 Should it be later?应该晚点吗? like onResume?
喜欢 onResume?
When opened through Google Assistant, the android.intent.extra.REFERRER_NAME
will be android-app://com.google.android.googlequicksearchbox/https/www.google.com
通过 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
}
Based on the response that theapache64 gave and this link: https://github.com/allegro/slinger/blob/master/slinger/src/main/java/pl/allegro/android/slinger/ReferrerMangler.java基于theapache64给出的响应和这个链接: https://github.com/allegro/slinger/blob/master/slinger/src/main/java/pl/allegro/android/slinger/ReferrerMangler.Z93F725A07423FE1C889F448B33D21F46
Because the intent was returning null on Android 10, and due to my min SDK being 23 (I do not need to implement logic for under M), I have done the following code:因为意图是在 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
}
This being Kotlin, and being in an activity.这是 Kotlin,并且正在进行活动。 The equivalent of referrer in.java would be:
相当于 in.java 的推荐人将是:
activity.getReferrer();
In case you run an OS under 23, the referrer can be taken like this:如果您在 23 岁以下运行操作系统,则可以像这样使用引荐来源网址:
val KEY_REF_NAME = "android.intent.extra.REFERRER_NAME"
intent.extras?.get(KEY_REF_NAME)
Being that theapache64 tried on a OnePlus6, I assume this should work until API level 28 (Pie) on some devices.由于 theapache64 在 OnePlus6 上尝试过,我认为这应该在某些设备上的 API 级别 28(Pie)之前有效。 But to be sure, I would recommend using the
activity.getReferrer()
但可以肯定的是,我建议使用
activity.getReferrer()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.