【问题标题】:Receive Listener onResults() from a different activity从不同的活动接收监听器 onResults()
【发布时间】: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


    【解决方案1】:

    尝试先定义一个接口:

    public interface RecognitionCallback
    {
       abstract void onRecoginitionFinished(ArrayList<String> matches);
    }
    

    现在让你需要回调的活动实现这个接口。例如:

    public class MainActivity extends AppCompatActivity implements RecognitionCallback {
    
      ...
    
      public void onRecognitionFinished(ArrayList<String> matches)
      {
         //do your things with the data
      }
    
    }
    

    同时添加 SpeechRecognitionListener 类的一些属性:

    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);
    

    【讨论】:

    • 我同意@DavidSeroussi!非常感谢 !我真的很菜鸟接口:P。顺便提一句!如果您对 SpeechRecognition 有所了解,可以查看我的上一篇文章,如果有人帮助我,我将不胜感激!
    • @FranciscoDurdinGarcia 我不熟悉语音识别,但在编码实现中如果有任何问题我很乐意提供帮助
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 2015-10-07
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多