简体   繁体   English

我怎么知道我的应用是由 Google 助理打开的,而不是正常启动的

[英]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]

EDIT:编辑:

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?但我的问题是,当我从机器构建应用程序或更新它时,这也会运行,知道如何避免这种情况吗?

EDIT2:编辑2:

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.

相关问题 我怎么知道我的应用程序的应用程序信息屏幕已打开 - How can I know that App info screen of My Application is opened 我怎么知道那个特定的应用程序。 在android中通过我的应用程序启动 - how can I know that certain app. in android is launched through my Application Android Google Assistant 只用应用名称打开我的应用 - Android Google Assistant open my app with just app name Android:如何知道是否可以启动应用 - Android: How to know if an app can be launched 使用Google API时,如何在应用程序中获得仅城市名称而不是整个地址的输出? - How can i get the output as just the city name instead of whole address in my App when using Google API? 我如何知道用户是否已安装并打开我的应用程序? - How do I know if a user has my app installed and opened? 我怎么知道该用户刚刚续订了 google play 订阅? - How can I know that user just renewed a google play subscription? 我可以设置Google Assistant以使用Dialogflow向我的Android应用发送推送通知吗? - Can I set up Google Assistant to send a push notification to my Android app using Dialogflow? 我怎么知道我的应用程序已被删除? - How can I know if my app is removed? 我可以将Google Assistant SDK与Google Home应用程序链接吗? - Can i link the google assistant sdk with the google home app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM