【发布时间】:2016-04-29 21:03:19
【问题描述】:
我正在使用SpeechRecognizer 开发一个应用程序。我将在不同的活动中为不同的用途使用它,并且一直将相同的代码添加到不同的类有点脏。所以我将我的自定义 RecognitionListener 移到了一个新类。这样,我只需在需要时从我的活动中初始化它。但我无法在我当前的活动中找到一种方法来接收听众的结果(在这种情况下,ArrayList 识别语音的可能值)来使用它。
我尝试通过接口实现它,但我认为我以错误的方式实现它。我的监听器代码是这样的:
public class SpeechRecognitionListener implements RecognitionListener
{
private final String TAG = "SpeechRecognitionListener";
private Intent mSpeechRecognizerIntent;
private SpeechRecognizer mSpeechRecognizer;
public SpeechRecognitionListener(Intent speechRecognizerIntent, SpeechRecognizer speechRecognizer ) {
mSpeechRecognizerIntent = speechRecognizerIntent;
mSpeechRecognizer = speechRecognizer;
}
@Override
public void onBeginningOfSpeech()
{
//Log.d(TAG, "onBeginingOfSpeech");
}
@Override
public void onBufferReceived(byte[] buffer)
{
}
@Override
public void onEndOfSpeech()
{
//Log.d(TAG, "onEndOfSpeech");
}
@Override
public void onError(int error)
{
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
//Log.d(TAG, "error = " + error);
}
@Override
public void onEvent(int eventType, Bundle params)
{
}
@Override
public void onPartialResults(Bundle partialResults)
{
}
@Override
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
}
@Override
public void onResults(Bundle results)
{
//I want to recieve this array in my main activity
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
@Override
public void onRmsChanged(float rmsdB)
{
}
}
我只想在我当前的活动中接收onResult() 数组以使用它。
【问题讨论】:
标签: android android-activity listener android-custom-view actionresult