In my Android app, I’d like to create separate listener of RecognitionListener, and I did it in a fine way as below:
lateinit var sr: SpeechRecognizer
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sr = SpeechRecognizer.createSpeechRecognizer(this)
sr.setRecognitionListener(Listener(this))
}
}
And Listner
as:
class Listener(context: Context): RecognitionListener {
init {
var ctx = context
}
override fun onReadyForSpeech(params: Bundle?) {
Log.d(TAG, "onReadyForSpeech")
Toast.makeText(ctx, "Ready!!", Toast.LENGTH_LONG).show()
}
}
In the Toast
, I’ve to use the context, this is why I passed it in the constructer, but unfortunately it is not recognized at the nReadyForSpeech
,
I defined it in the init {}
but did not work, how can I solve it, and pass the context from MainActivity
to this function