Android 将识别器侦听器的结果返回给调用它的类

Android 将识别器侦听器的结果返回给调用它的类,android,speech-recognition,Android,Speech Recognition,我在我的软键盘应用程序中使用SpeechRecogener和RecogenerListener。我可以从侦听器获得结果,但是触发结果自动添加到InputConnection是有问题的。。。这里有一些代码 public class SoftKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private SpeechRecognizer getMsg = Spee

我在我的软键盘应用程序中使用SpeechRecogener和RecogenerListener。我可以从侦听器获得结果,但是触发结果自动添加到InputConnection是有问题的。。。这里有一些代码

public class SoftKeyboard extends InputMethodService 
    implements KeyboardView.OnKeyboardActionListener {

private SpeechRecognizer getMsg = SpeechRecognizer.createSpeechRecognizer(this);
public static String mResult = "";


// a bunch of code that is irrelavent to my question   


public void getVoice () {
    mResult = "";
    context = this;
    if (isListening == false) {
        isListening = true;
            Intent intent = new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
            intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());        
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");        
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);        
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
            Talk doMsg = new Talk();
            getMsg.setRecognitionListener(doMsg);
            getMsg.startListening(intent);
    }

     }

    public void putText () {
        getCurrentInputConnection().commitText(mResult, 1);
    }
}

class Talk implements RecognitionListener {
Context context = SoftKeyboard.context;


public void onBeginningOfSpeech() {
    Toast.makeText(context, "Beginning of Speech", Toast.LENGTH_SHORT).show();

}

public void onBufferReceived(byte[] buffer) {
    //Toast.makeText(context, "Buffer received", Toast.LENGTH_SHORT).show();


}

public void onEndOfSpeech() {
    Toast.makeText(context, "End Of Speech", Toast.LENGTH_SHORT).show();

}

public void onError(int error) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "There was an Error: "+String.valueOf(error), Toast.LENGTH_SHORT).show();
    SoftKeyboard.isListening = false;
}

public void onEvent(int eventType, Bundle params) {
    Toast.makeText(context, "Event: "+String.valueOf(eventType), Toast.LENGTH_SHORT).show();


}

public void onPartialResults(Bundle partialResults) {
    Toast.makeText(context, "Partial Results to be had", Toast.LENGTH_SHORT).show();

}

public void onReadyForSpeech(Bundle params) {
    Toast.makeText(context, "Ready For Speech!", Toast.LENGTH_SHORT).show();


}

public void onResults(Bundle results) {
    ArrayList<String> result = results.getStringArrayList("results_recognition");
    if (result.isEmpty() == false) {
        for (int i = 0; i<(result.size()-1);i++) {
            Toast.makeText(context, result.get(i), Toast.LENGTH_SHORT).show();
        }
        SoftKeyboard.mResult = result.get(0);
    } else {
        Toast.makeText(context, "no results", Toast.LENGTH_SHORT).show();
        SoftKeyboard.mResult = "-1";
    }
    SoftKeyboard.isListening = false;
    SoftKeyboard.putText();
}

public void onRmsChanged(float rmsdB) {
}

}

所以我通过按下按钮调用getVoice方法,它工作正常,直到我在软键盘中调用putText方法。我只能从onResult方法调用静态方法,但如果我将putText设置为静态方法,则无法调用getCurrentInputConnection。。。我发现的唯一方法是让用户再次单击按钮以插入文本,但这对用户不是很友好。我想在侦听器中的onResult方法运行后自动调用getCurrentInputConnection,但是静态/非静态冲突在这里非常麻烦。任何帮助都将不胜感激!谢谢大家!

尝试添加额外的部分结果。有帮助吗?

谢谢你的建议。我试过了,但没用。我的问题是doMsg是一个静态对象,因此当我尝试从RecognitionListener doMsg中的onResults方法执行非静态方法getCurrentInputConnection.CommitteText时,这是不允许的。我需要一种使用静态对象执行非静态命令的方法。或者我需要使doMsg非静态。我试图在doMsg设置mResult的值时实现一个侦听器,但无法实现。因此,我的最佳解决方案是要求用户按下按钮插入文本。