Android 从其他活动接收侦听器onResults()
我正在开发一个带有Android 从其他活动接收侦听器onResults(),android,android-activity,listener,android-custom-view,actionresult,Android,Android Activity,Listener,Android Custom View,Actionresult,我正在开发一个带有语音识别器的应用程序。我将在不同的活动中为不同的用途使用它,而且它有点脏,始终将相同的代码添加到不同的类中。因此,我将自定义的RecognitionListener移动到一个新类。这样,我只需在需要从活动中初始化它。但是在我当前的活动中,我找不到一种方法来接收侦听器的结果(在本例中,是识别语音的可能值的ArrayList) 我试图通过一个接口来实现它,但我认为我用了一种错误的方式。我的侦听器代码如下: public class SpeechRecognitionListener
语音识别器的应用程序。我将在不同的活动中为不同的用途使用它,而且它有点脏,始终将相同的代码添加到不同的类中。因此,我将自定义的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)
{
}
}
公共类SpeechRecognitionListener实现了RecognitionListener
{
私有最终字符串标记=“SpeechRecognitionListener”;
私人意图mSpeechRecognizerIntent;
私人语音识别器;
公共语音识别侦听器(意图语音识别器意图,语音识别器语音识别器){
mSpeechRecognizerIntent=语音识别器Intent;
mSpeechRecognizer=语音识别器;
}
@凌驾
开始时的公共无效fSpeech()
{
//Log.d(标签“onBeginingOfSpeech”);
}
@凌驾
已接收公共无效onBufferReceived(字节[]缓冲区)
{
}
@凌驾
公共无效onEndOfSpeech()
{
//Log.d(标签“onEndOfSpeech”);
}
@凌驾
公共无效onError(内部错误)
{
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
//Log.d(标记“error=“+error”);
}
@凌驾
public void onEvent(int eventType,Bundle参数)
{
}
@凌驾
public void onPartialResults(Bundle partialResults)
{
}
@凌驾
ReadyForSpeech上的公共void(Bundle参数)
{
Log.d(标记“onReadyForSpeech”);//$NON-NLS-1$
}
@凌驾
公共结果(捆绑结果)
{
//我想在我的主要活动中接收此数组
ArrayList matches=results.getStringArrayList(SpeechRecognitor.results\u RECOGNITION);
}
@凌驾
在RMSCHANGED上的公共无效(浮动rmsdB)
{
}
}
我只想在当前活动中接收onResult()
数组以使用它。首先尝试定义一个接口:
public interface RecognitionCallback
{
abstract void onRecoginitionFinished(ArrayList<String> matches);
}
首先尝试定义一个接口:
public interface RecognitionCallback
{
abstract void onRecoginitionFinished(ArrayList<String> matches);
}
我同意@davidserosi!非常感谢你!我真的没有界面:顺便说一句!如果你对语音识别有所了解,你可以查看我的上一篇帖子,如果有人能帮我,我将非常感激@FranciscoDurdinGarcia我不熟悉语音识别,但在编码实现方面,如果有任何问题,我很乐意帮助我同意@DavidSurosi!非常感谢你!我真的没有界面:顺便说一句!如果你对语音识别有所了解,你可以查看我的上一篇帖子,如果有人能帮我,我将非常感激@FranciscoDurdinGarcia我不熟悉语音识别,但在编码实现方面,如果有任何问题,我很乐意提供帮助
public class SpeechRecognitionListener implements RecognitionListener
{
private final String TAG = "SpeechRecognitionListener";
private Intent mSpeechRecognizerIntent;
private SpeechRecognizer mSpeechRecognizer;
private RecognitionCallback mCallback
public SpeechRecognitionListener(Intent speechRecognizerIntent, SpeechRecognizer speechRecognizer, RecognitionCallback callback ) {
mSpeechRecognizerIntent = speechRecognizerIntent;
mSpeechRecognizer = speechRecognizer;
mCallback = callback;
...
public void onResults(Bundle results)
{
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
mCallback.onRecognitionFinished(matches);
}
}
SpeechRecognitionListener listener = new SpeechRecognitionLinstner(intent,recognizer,this);