无法将上下文转换为 RecognitionListener

Context cannot be converted to RecognitionListener

我正在 Cordova 中实现语音识别插件。
为此我得到了错误

incompatible types: Context cannot be converted to RecognitionListener

在构建 cordova 插件时。
此错误来自以下代码

context = this.cordova.getActivity();
SpeechRecognizer speech = SpeechRecognizer.createSpeechRecognizer(context);
speech.setRecognitionListener(context); // Getting error here

有解决办法吗?

您的 Activity 需要 implements RecognitionListener 并且您必须将通用 Context 对象转换为该接口。

context =  this.cordova.getActivity();
SpeechRecognizer speech = SpeechRecognizer.createSpeechRecognizer(context);
speech.setRecognitionListener(( RecognitionListener) context); 

或者您可以传递匿名 class

context =  this.cordova.getActivity();
SpeechRecognizer speech = SpeechRecognizer.createSpeechRecognizer(context);
speech.setRecognitionListener(new RecognitionListener() {});