Android 如何在自定义键盘中添加语音识别

Android 如何在自定义键盘中添加语音识别,android,Android,我正在开发一个工作正常的自定义软键盘。现在我想添加语音识别(STT)功能我正在使用android自定义api。但问题是,当我按下麦克风按钮时,会出现语音识别器,我会得到与语音对应的字符串。但如何在onkey事件中再次发送以打印它。我不知道。请帮助我。 代码如下: public void onKey(int pc, int[] key) { ic = getCurrentInputConnection(); al.clear(); //cv.clea

我正在开发一个工作正常的自定义软键盘。现在我想添加语音识别(STT)功能我正在使用android自定义api。但问题是,当我按下麦克风按钮时,会出现语音识别器,我会得到与语音对应的字符串。但如何在onkey事件中再次发送以打印它。我不知道。请帮助我。 代码如下:

public void onKey(int pc, int[] key) {
        ic = getCurrentInputConnection();
        al.clear();
        //cv.clear();
        switch (pc) {
        case 45:
                    Speech.i=false;
        Speech.ic=ic;
            Intent intent = new Intent(getApplicationContext(),Speech.class);
            intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getCurrentInputConnection().commitText(Speech.spoken, 1);
}
下面是演讲课:-

public class Speech extends Activity {
    static ArrayList<String> results;
    static float[] confidence;
    public static String spoken;
    public static boolean i = true;
    public static InputConnection ic;
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        // Specify free form input
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "or forever hold your peace");
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
        startActivityForResult(intent, 5);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 5 && resultCode == RESULT_OK) {

            results = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            String confidenceExtra = RecognizerIntent.EXTRA_CONFIDENCE_SCORES;
            confidence = data.getFloatArrayExtra(confidenceExtra);
            // TODO Do something with the recognized voice strings
        }

        spoken = null;
        spoken=getWord();
        Main.ic.commitText(spoken, 1);
        //Main m =new Main();
        //m.onKey(28, key)
        //broadcastIntent();
        finish();

    }

    public  String getWord() {
        float max = confidence[0];
        int j = 0;
        for (int i = 0; i < results.size(); i++) {
            if (max < confidence[i]) {
                j = i;
                max = confidence[i];
            }
        }
        //Log.i("main", "" + spoken);
        i = true;
        spoken=results.get(j);
        Log.i("main", "" + spoken);
        return spoken;
    }
/*  public void broadcastIntent()
    {
        Intent intent =new Intent();
        intent.setAction("ttsdone");
        sendBroadcast(intent);
    }*/

}
公共课堂演讲扩展活动{
静态数组列表结果;
静态浮动[]置信度;
公共静态字符串;
公共静态布尔值i=true;
公共静态输入连接ic;
@凌驾
创建时的公共void(Bundle b){
super.onCreate(b);
意向意向=新意向(识别意向、行动、识别言语);
//指定自由格式输入
intent.putExtra(识别器intent.EXTRA_语言_模型,
识别者意图、语言、模型、自由形式);
intent.putExtra(识别器intent.EXTRA\u提示符,
“或者永远保持沉默”);
intent.putExtra(识别器intent.EXTRA_MAX_结果,1);
intent.putExtra(RecognizerIntent.EXTRA_语言,Locale.ENGLISH);
startActivityForResult(意向书,5);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==5&&resultCode==RESULT\u确定){
结果=数据
.getStringArrayListExtra(识别器意图.额外结果);
String confidenceExtra=RecognizerIntent.EXTRA\u CONFIDENCE\u分数;
信心=data.getFloatArrayExtra(信心额外);
//TODO使用已识别的语音字符串执行某些操作
}
口语=零;
口语=getWord();
主要ic委员会文本(口语,1);
//Main m=新的Main();
//m、 ON键(28,键)
//广播意图();
完成();
}
公共字符串getWord(){
浮动最大值=置信度[0];
int j=0;
对于(int i=0;i
我猜你现在已经解决了这个问题,但我会继续发帖,以防对其他人有所帮助。我解决这个问题的方法是将文本字符串保存在SharedReferences中,然后在IME类的onStartInput方法启动时检索该字符串。也许有更好的方法可以做到这一点,但这就是我所能想到的。我没有使用keycode来提交字符串,我使用了commitText方法,如下所示:

 getCurrentInputConnection().commitText("the spoken string here", 1);