从另一个 activity 接收监听器 onResults()
Receive Listener onResults() from a different activity
我正在使用 SpeechRecognizer
开发应用程序。我将在不同的活动中使用它来实现不同的用途,并且始终将相同的代码添加到不同的 classes 有点脏。所以我将我的自定义 RecognitionListener
移到了新的 class。这样我就可以在我的活动中需要时初始化它。但是我无法在当前 activity 中找到一种方法来接收听众的结果(在这种情况下,识别语音的 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)
{
}
}
我只想在当前 activity 中接收 onResult()
数组来使用它。
先尝试定义一个接口:
public interface RecognitionCallback
{
abstract void onRecoginitionFinished(ArrayList<String> matches);
}
现在让你需要回调的activity实现这个接口。例如:
public class MainActivity extends AppCompatActivity implements RecognitionCallback {
...
public void onRecognitionFinished(ArrayList<String> matches)
{
//do your things with the data
}
}
同时添加 SpeechRecognitionListener 的一些属性 class:
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);
}
}
最后在您需要回电的 activity 处写下:
SpeechRecognitionListener listener = new SpeechRecognitionLinstner(intent,recognizer,this);
我正在使用 SpeechRecognizer
开发应用程序。我将在不同的活动中使用它来实现不同的用途,并且始终将相同的代码添加到不同的 classes 有点脏。所以我将我的自定义 RecognitionListener
移到了新的 class。这样我就可以在我的活动中需要时初始化它。但是我无法在当前 activity 中找到一种方法来接收听众的结果(在这种情况下,识别语音的 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)
{
}
}
我只想在当前 activity 中接收 onResult()
数组来使用它。
先尝试定义一个接口:
public interface RecognitionCallback
{
abstract void onRecoginitionFinished(ArrayList<String> matches);
}
现在让你需要回调的activity实现这个接口。例如:
public class MainActivity extends AppCompatActivity implements RecognitionCallback {
...
public void onRecognitionFinished(ArrayList<String> matches)
{
//do your things with the data
}
}
同时添加 SpeechRecognitionListener 的一些属性 class:
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);
}
}
最后在您需要回电的 activity 处写下:
SpeechRecognitionListener listener = new SpeechRecognitionLinstner(intent,recognizer,this);