Q:

Voice trigger to start recognition in android application

How to use a voice trigger to open recognitionIntent?  Run code when user say "coke" :
Intent it = new Intent(RecognizerIntent. ACTION_RECOGNIZE_SPEECH);
it.putExtra(RecognizerIntent. EXTRA_LANGUAGE_MODEL, RecognizerIntent. LANGUAGE_MODEL_FREE_FORM);
it.putExtra(RecognizerIntent. EXTRA_PROMPT, "請說...");
this.startActivityForResult(it, 1);

A:

private final int REQ_CODE_SPEECH_INPUT = 100;

private void promptSpeechInput() {

    Intent intent = new Intent(RecognizerIntent. ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent. EXTRA_LANGUAGE_MODEL,
            RecognizerIntent. LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent. EXTRA_LANGUAGE,"en_US");
    intent.putExtra(RecognizerIntent. EXTRA_PROMPT,"請說...");
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        //speech_not_supported
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == Activity. RESULT_OK && null ! = data) {
                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent. EXTRA_RESULTS);

                Log.v("tag","result: " + result.get(0)); 

                if(result.get(0).equals("coke")){
                    //do something
                }
            }
            break;
        }
    }
}

