Is there a way to launch voice input on the softkeyboard programatically?

丶灬走出姿态 提交于 2019-12-05 22:27:26

No, it's not possible. At least, there is no simple way to launch voice input without using RecognitionListener or manually clicking the voice input button on soft keyboard.

As far as I know, this post explains how to integrate voice into an IME which actually uses this library: google-voice-typing-integration. It might inspire you a bit.

Silas Greenback

Sorry to have answered my own question but here it was: I took the whole "grab a string array from voice input" off of the link in the question, then, instead of just starting a search with onSearchRequested(), I did this:

startSearch(grabString, false, null, false);


public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(aViaBuildConfig.MIC_KEY) {
        DebugLog.e(TAG , "onDown event : " + event);
        DebugLog.e(TAG , "onDown keyCode: " + keyCode);
        if(keyCode == Constants.MIC_KEY) {
            onSearchRequested();
            Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
            try {
                startActivityForResult(voiceIntent, Constants.RESULT_SPEECH);
            } catch (ActivityNotFoundException ex) {
                DebugLog.e(TAG, "Not found excpetion onKeyDown: " + ex);
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    return false; 
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case Constants.RESULT_SPEECH:
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK && null != data) {
                 ArrayList<String> spokenSearch = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                 DebugLog.e("Glenn: " , "Speech = " + spokenSearch);
                 String grabString = spokenSearch.get(0);
                 startSearch(grabString, false, null, false);
            }
            break;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!