How can I know that my app was opened by Google Assistant, instead of just normally launched

3

How can I know that my app was opened by Google Assistant, instead of just normally launched. 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. 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:

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:

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

I am trying the : intent.extras?.get(KEY_REF_NAME) == REG_G_ASSISTANT or getReferrerCompatible() from the onCreate. Should it be later? like onResume?

android
speech-recognition
voice-recognition
google-assistant-sdk
google-assist-api
asked on Stack Overflow Nov 14, 2019 by rosu alin • edited Nov 14, 2019 by rosu alin

2 Answers

1

When opened through Google Assistant, the android.intent.extra.REFERRER_NAME will be 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
}

answered on Stack Overflow Nov 14, 2019 by theapache64
0

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

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:

  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. The equivalent of referrer in .java would be:

activity.getReferrer(); 

In case you run an OS under 23, the referrer can be taken like this:

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. But to be sure, I would recommend using the activity.getReferrer()

answered on Stack Overflow Nov 14, 2019 by rosu alin

User contributions licensed under CC BY-SA 3.0