Search code examples
androidgoogle-glassgoogle-gdk

Multiple voice triggers to start the same Activity on Glass and distinguish which one was used


I would like to use two different voice triggers to open the same activity, and inside this activity, decide what to do depending on which trigger was used.

Is this possible without adding an extra prompt? According to the docs, you have access to the RecognizerIntent.EXTRA_RESULTS only if a prompt is used.

So my question, is it possible to fire the same activity with more than one voice trigger, and is there any way to know in code which trigger was used?


Solution

  • After doing what @Ferdau said, I found a better way with activity-alias and meta-data.

    Add an activity that contains the first voice trigger to your AndroidManifest.xml:

    <activity android:name="com.package.MainActivity">
        <intent-filter>
            <action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
        </intent-filter>
        <meta-data
            android:name="com.google.android.glass.VoiceTrigger"
            android:resource="@xml/glass_first_trigger" />
    </activity>
    

    Then after that, add an activity-alias that contains the second trigger

    <activity-alias
        android:name=".StartMainActivityWithAParameter"
        android:targetActivity="com.package.MainActivity">
        <intent-filter>
            <action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
        </intent-filter>
        <meta-data
           android:name="com.google.android.glass.VoiceTrigger"
           android:resource="@xml/glass_second_trigger" />
    </activity-alias>
    

    Then, on code, you can read meta-data values and decide what to do:

    ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(),
    PackageManager.GET_ACTIVITIES|PackageManager.GET_META_DATA);
    int secondVoiceTrigger = activityInfo.metaData.getInt("com.google.android.glass.VoiceTrigger");
    if(secondVoiceTrigger == R.xml.glass_second_trigger) {
          //TODO do different stuff
    }