3

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?

Fernando Gallego
  • 4,064
  • 31
  • 50

2 Answers2

4

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
}
Community
  • 1
  • 1
Fernando Gallego
  • 4,064
  • 31
  • 50
  • @fredy182 Just ran into a similar issue right now with something I'm working on. This solution looks great, I'm going to give it a shot now. Did you keep it like this in the end or did you end up refactoring it some other way? – Stephen Tetreault Oct 13 '14 at 19:30
  • 1
    @SMT I am using it like this, I also added an android:enabled attribute with a placeholder to enable/disable it per gradle flavor, so it will show up only for Glass and not for other platforms – Fernando Gallego Oct 14 '14 at 10:14
  • @fredy182 awesome, that's a great idea. I put this in place yesterday and its working for me, thanks! – Stephen Tetreault Oct 14 '14 at 13:42
0

Well you could actually just create a dummy Activity that just calls your main Activity...

Voice Trigger 1 -> MainActivity
Voice Trigger 2 -> DummyActivity -> MainActivity (with extra parameter)
Ferdau
  • 1,524
  • 1
  • 12
  • 21