androidspeech-recognitionvoice-recognitiongoogle-assistant-sdkgoogle-assist-api

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. 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?


Solution

  • 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
    }