android中VoiceRecognition的使用
来源:互联网 发布:阿里云 那个机房快 编辑:程序博客网 时间:2024/05/16 15:22
使用此类前需要安装一个Voice Search,地址http://voice-search.softonic.cn/android/download#downloading
package com.example.androidapi.app;import java.util.ArrayList;import java.util.List;import android.app.Activity;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.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import com.example.androidapi.R;public class VoiceRecognition extends Activity implements OnClickListener { private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; private ListView mList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.voice_recognition); Button speakButton = (Button) findViewById(R.id.btn_speak); mList = (ListView) findViewById(R.id.list); //检查包中是否存在此apk PackageManager pm = getPackageManager(); List<ResolveInfo> activities = pm.queryIntentActivities( new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); if (activities.size() != 0) { speakButton.setOnClickListener(this); } else { speakButton.setEnabled(false); speakButton.setText("Recognizer not present"); } } public void onClick(View v) { if (v.getId() == R.id.btn_speak) { startVoiceRecognitionActivity(); } } //启动谷歌的语音服务 private void startVoiceRecognitionActivity() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo"); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { ArrayList<String> matches = data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS); mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches)); } super.onActivityResult(requestCode, resultCode, data); }}
- android中VoiceRecognition的使用
- android VoiceRecognition 语音识别并打印到列表上
- Android中ListActivity的使用
- Android 中LayoutInflater的使用
- Android中ExpandableListView的使用
- Android中Handler的使用
- android中ProgressBar的使用
- Android中ListView的使用
- android中ImageButton的使用
- Android中Spinner的使用
- Android中ContextMenu的使用
- Android中Preference的使用
- Android 中LayoutInflater的使用
- Android中webview的使用
- android中sqlite的使用
- Android中shape的使用
- android中Animation的使用!
- android中LayoutInflater的使用
- FFMpeg中apiexample.c例子分析——解码分析
- 小笔记
- source\lib\math1.h.c__常用数学函数
- paip.破解网站手机验证码
- 使用Struts 2将Java对象序列化成JSON
- android中VoiceRecognition的使用
- android intent的理解
- 汇编02-8086/8088寻址方式和指令系统
- android Button 监听的几种方式
- IOS开发之手势——UIGestureRecognizer 共存
- 原版的firefox
- 用marquee和div+js实现首尾相连循环滚动效果
- MSSQL锁定-3.死锁与阻塞(myBased)
- MS SQL 用户登录信息查询