Search code examples
androidsearchandroid-intentvoice

Pass context to a voice search


During a search you can pass some data to search manager by using the APP_DATA bundle. This mechanism works great for normal search, but how can you do the same for a voice search and get back some context information when he voice search returns ?


Solution

  • From what I understand, it goes through the same mechanism. Simply create your normal onSearchRequested override in the backend, then annotate your dialog or widget with voice search functionality as described here.

    Using their example, something like this should go into your frontend:

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/search_label"
        android:hint="@string/search_hint"
        android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >
    </searchable>
    

    When voice search is requested, its data will be passed through the search mechanism and on to your onSearchRequested callback, allowing you to manipulate the data as needed.


    Edit: The actual problem being addressed here was to differentiate when voice search is used in a search widget versus when the standard text input has been invoked.

    Unfortunately, it appears Google does not provide for these facilities unless you roll your own Recognizer or attempt to retrieve properties from the search bundle that are shaped like voice data. The latter case is undocumented and, at least apparently, is unsupported as well.