Android 有人能帮我纠正一下这个安卓语音识别课程吗?
我正在分析这个语音识别器类,需要帮助理解代码中的这一行。以下行的作用是什么?它是否创建了安卓平台上加载到设备上的所有活动的列表?我特别觉得解析信息有点混乱 列出活动=pm.QueryEntActivities(新意图( 下面是上下文中使用的代码Android 有人能帮我纠正一下这个安卓语音识别课程吗?,android,Android,我正在分析这个语音识别器类,需要帮助理解代码中的这一行。以下行的作用是什么?它是否创建了安卓平台上加载到设备上的所有活动的列表?我特别觉得解析信息有点混乱 列出活动=pm.QueryEntActivities(新意图( 下面是上下文中使用的代码 package com.example.voicerecognitionactivity; import java.util.ArrayList; import java.util.List; import android.app.Activity;
package com.example.voicerecognitionactivity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
public class VoiceRecognitionActivity extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
private EditText metTextHint;
private ListView mlvTextMatches;
private Spinner msTextMatches;
private Button mbtSpeak;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
metTextHint = (EditText) findViewById(R.id.etTextHint);
mlvTextMatches = (ListView) findViewById(R.id.lvTextMatches);
msTextMatches = (Spinner) findViewById(R.id.sNoOfMatches);
mbtSpeak = (Button) findViewById(R.id.btSpeak);
}
public void checkVoiceRecognition() {
// Check if voice recognition is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
mbtSpeak.setEnabled(false);
Toast.makeText(this, "Voice recognizer not present",
Toast.LENGTH_SHORT).show();
}
}
public void speak(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Specify the calling package to identify your application
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
.getPackage().getName());
// Display an hint to the user about what he should say.
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, metTextHint.getText()
.toString());
// Given an hint to the recognizer about what the user is going to say
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
// If number of Matches is not selected then return show toast message
if (msTextMatches.getSelectedItemPosition() == AdapterView.INVALID_POSITION) {
Toast.makeText(this, "Please select No. of Matches from spinner",
Toast.LENGTH_SHORT).show();
return;
}
int noOfMatches = Integer.parseInt(msTextMatches.getSelectedItem()
.toString());
// Specify how many results you want to receive. The results will be
// sorted where the first result is the one with higher confidence.
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, noOfMatches);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
//If Voice recognition is successful then it returns RESULT_OK
if(resultCode == RESULT_OK) {
ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (!textMatchList.isEmpty()) {
// If first Match contains the 'search' word
// Then start web search.
if (textMatchList.get(0).contains("search")) {
String searchQuery = textMatchList.get(0).replace("search",
" ");
Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
search.putExtra(SearchManager.QUERY, searchQuery);
startActivity(search);
} else {
// populate the Matches
mlvTextMatches
.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
textMatchList));
}
}
//Result code for various error.
}else if(resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){
showToastMessage("Audio Error");
}else if(resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){
showToastMessage("Client Error");
}else if(resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){
showToastMessage("Network Error");
}else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){
showToastMessage("No Match");
}else if(resultCode == RecognizerIntent.RESULT_SERVER_ERROR){
showToastMessage("Server Error");
}
super.onActivityResult(requestCode, resultCode, data);
}
void showToastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
package com.example.voicerecognitionactivity;
导入java.util.ArrayList;
导入java.util.List;
导入android.app.Activity;
导入android.app.SearchManager;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.content.pm.ResolveInfo;
导入android.os.Bundle;
导入android.speech.RecognizerIntent;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.Spinner;
导入android.widget.Toast;
公共类VoiceRecognitionActivity扩展了活动{
专用静态最终int语音识别请求代码=1001;
私有EditText-metTextHint;
私有ListView mlvTextMatches;
私有微调器msTextMatches;
专用按钮mbtSpeak;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
metTextHint=(EditText)findViewById(R.id.etTextHint);
mlvTextMatches=(ListView)findViewById(R.id.lvTextMatches);
msTextMatches=(微调器)findViewById(R.id.sNoOfMatches);
mbtSpeak=(按钮)findviewbyd(R.id.btSpeak);
}
公共无效检查语音识别(){
//检查是否存在语音识别
PackageManager pm=getPackageManager();
列出活动=pm.QueryEntActivities(新意图(
识别器意图。动作识别(言语),0);
如果(activities.size()==0){
mbtSpeak.setEnabled(false);
Toast.makeText(此“语音识别器不存在”,
吐司。长度(短)。show();
}
}
公开演讲(视图){
意向意向=新意向(识别意向、行动、识别言语);
//指定调用包以标识应用程序
intent.putExtra(RecognizerIntent.EXTRA_调用_包,getClass()
.getPackage().getName());
//向用户显示关于他应该说什么的提示。
intent.putExtra(RecognizerIntent.EXTRA\u提示符,metTextHint.getText()
.toString());
//向识别器提示用户将要说什么
intent.putExtra(识别器intent.EXTRA_语言_模型,
识别者意图、语言、模型、网络搜索);
//如果未选择匹配数,则返回ShowToast消息
if(msTextMatches.getSelectedItemPosition()==AdapterView.INVALID_位置){
Toast.makeText(这是“请从微调器中选择匹配数”,
吐司。长度(短)。show();
返回;
}
int noOfMatches=Integer.parseInt(msTextMatches.getSelectedItem()
.toString());
//指定要接收的结果数量。结果将为
//在第一个结果是置信度较高的结果时进行排序。
intent.putExtra(识别intent.EXTRA_最大结果,无匹配);
startActivityForResult(意图、语音识别、请求、代码);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(请求代码==语音识别请求代码)
//如果语音识别成功,则返回结果\u OK
if(resultCode==结果\u OK){
ArrayList textMatchList=数据
.getStringArrayListExtra(识别器意图.额外结果);
如果(!textMatchList.isEmpty()){
//如果第一个匹配项包含“搜索”字
//然后开始网络搜索。
if(textMatchList.get(0.contains)(“搜索”)){
String searchQuery=textMatchList.get(0).replace(“搜索”,
" ");
意向搜索=新意向(意向.行动\网络\搜索);
search.putExtra(SearchManager.QUERY,searchQuery);
星触觉(搜索);
}否则{
//填充匹配项
mlvTextMatches
.setAdapter(新阵列适配器)(此,
android.R.layout.simple\u list\u item\u 1,
文本匹配列表);
}
}
//各种错误的结果代码。
}else if(resultCode==RecognizerIntent.RESULT\u AUDIO\u ERROR){
showToastMessage(“音频错误”);
}else if(resultCode==RecognizerIntent.RESULT\u CLIENT\u ERROR){
showToastMessage(“客户端错误”);
}else if(resultCode==RecognizerIntent.RESULT\u网络\u错误){
showToastMessage(“网络错误”);
}else if(resultCode==RecognizerIntent.RESULT\u不匹配){
showToastMessage(“不匹配”);
}else if(resultCode==RecognizerIntent.RESULT\u服务器\u错误){
showToastMessage(“服务器错误”);
}
super.onActivityResult(请求代码、结果代码、数据);
}
无效showToastMessage(字符串消息){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
}
我认为,这是检查是否存在语音识别的最简单方法:
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_LONG);
t.show();
}
在那之后:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
et_task_notes.setText(text.get(0));
}
break;
}
}
}
@覆盖
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(r