MainActivity.kt
4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.naji.one_key
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
import android.os.Bundle
import android.util.Log
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodCall
import android.content.Intent
import android.speech.RecognizerIntent
class MainActivity: FlutterActivity() {
private var TAG = "| com.naji.book |"
// 通道名称
private var CHANNEL = "com.naji/book"
// native端的Intent数据
private var intentMap = mutableMapOf<String, String>()
// 语音转文字结果回调
private var pendingSpeechResult: MethodChannel.Result? = null
private val SPEECH_REQUEST_CODE = 77012
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// flutter版本更新后使用 getFlutterEngine().getDartExecutor().getBinaryMessenger()代替 getFlutterView()
val channel = MethodChannel(getFlutterEngine()?.getDartExecutor()?.getBinaryMessenger()!!, CHANNEL)
// 这里要获取启动时的intent
val uri = intent.data
if(intent.data != null){
Log.i("flutter","onCreate"+intent.data.toString())
}
val paramsMap = mutableMapOf<String, String>()
if (uri != null) {
val set = uri.queryParameterNames
for (name in set) {
val value = uri.getQueryParameter(name)
paramsMap[name.toString()] = value.toString()
}
}
intentMap = paramsMap
channel.setMethodCallHandler { call, result ->
Log.i(TAG,intentMap.toString());
when (call.method) {
"getIntent" -> get_intent(call, result)
"startSpeechRecognition" -> start_speech_recognition(call, result)
}
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
if(intent.data != null){
Log.i("flutter","onNewIntent"+intent.data.toString())
}
// flutter版本更新后使用 getFlutterEngine().getDartExecutor().getBinaryMessenger()代替 getFlutterView()
val channel = MethodChannel(getFlutterEngine()?.getDartExecutor()?.getBinaryMessenger()!!, CHANNEL)
// 这里要获取启动时的intent
val uri = intent.data
val paramsMap = mutableMapOf<String, String>()
if (uri != null) {
val set = uri.queryParameterNames
for (name in set) {
val value = uri.getQueryParameter(name)
paramsMap[name.toString()] = value.toString()
}
}
intentMap = paramsMap
channel.invokeMethod("getIntent",intentMap)
// channel.setMethodCallHandler { call, result ->
// Log.i(TAG,intentMap.toString());
// when (call.method) {
// "getIntent" -> get_intent(call, result)
// }
// }
}
// 获取intent
private fun get_intent(call: MethodCall, result: MethodChannel.Result) {
Log.i(TAG, "调用${call.method}方法")
result.success(intentMap)
intentMap.clear();
}
// 打开系统语音识别界面,识别结果通过 onActivityResult 回传
private fun start_speech_recognition(call: MethodCall, result: MethodChannel.Result) {
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
putExtra(RecognizerIntent.EXTRA_LANGUAGE, "zh-CN")
putExtra(RecognizerIntent.EXTRA_PROMPT, "请说出要咨询的问题")
}
try {
pendingSpeechResult = result
startActivityForResult(intent, SPEECH_REQUEST_CODE)
} catch (e: Exception) {
pendingSpeechResult = null
result.error("SPEECH_UNAVAILABLE", e.message, null)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode != SPEECH_REQUEST_CODE) return
val result = pendingSpeechResult
pendingSpeechResult = null
if (result == null) return
if (resultCode == RESULT_OK && data != null) {
val matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
result.success(matches?.firstOrNull() ?: "")
} else {
result.success("")
}
}
}