如何让Google语音在Bound服务的后台持续运行并获取Activity的数据?

时间:2018-06-12 07:41:38

标签: android voice-recognition

我在互联网上搜索过但是,我无法找到这个解决方案的答案。当我启动我的应用程序并显示屏幕时,我想通过我的语音命令来控制我的APP,只需在后台运行识别器意图并将结果输入活动。 任何人都可以帮助我。 我看过这些链接,但我无法理解它? 1. How to make Google Voice run continually in the background on Android 2. https://codelabs.developers.google.com/codelabs/voice-interaction/index.html?index=..%2F..%2Findex#6

fun getSpeechInput() {

    val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())

    if (intent.resolveActivity(mContext!!.packageManager) != null) {
        startActivityForResult(intent, 10)
    } else {
        Toast.makeText(mContext, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show()
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    when (requestCode) {
        10 -> if (resultCode == Activity.RESULT_OK && data != null) {

            val result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)

            var voiceText : String = result[0]}

我想使用getspeechInput方法连续在后台运行,并在我的Activity中将结果输入onActivityResult。

0 个答案:

没有答案