在Android 1.5 SDK中已经加强了语音识别功能,第三方通过Intent就可以简单的使用这个功能!下面做简要说明!

先看页面布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" android:padding="5dip">    <Button        android:id="@+id/button1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="语音识别" />    <ListView        android:id="@+id/listView1"        android:layout_width="fill_parent"        android:layout_height="fill_parent" android:background="#00000000">    </ListView></LinearLayout>

说明:LinearLayout里面垂直放置了Button和ListView,通过Button的点击事件触发语音识别,然后把结果设置到ListView中。

Activity中的代码如下:

import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.speech.RecognizerIntent;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class RecognizerActivity extends Activity implements OnClickListener, OnItemClickListener {    private static final int VOICE_RECOGNITION_REQUEST_CODE = 131422;private ListView mListView;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                mListView = (ListView) findViewById(R.id.listView1);        mListView.setOnItemClickListener(this);                ((Button) findViewById(R.id.button1)).setOnClickListener(this);            }@Overridepublic void onClick(View v) {mListView.setAdapter(null);try {Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音识别");startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "不能语音识别", Toast.LENGTH_SHORT).show();}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && data != null) {ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);if (results != null) {mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));Toast.makeText(this, results.toString(), Toast.LENGTH_SHORT).show();super.onActivityResult(requestCode, resultCode, data);}}}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {Toast.makeText(this, parent.getAdapter().getItem(position).toString(), Toast.LENGTH_SHORT).show();}}

说明:

  • onCreate:获得相关View的引用,并设置对应的事件处理;
  • onClick:清空ListView数据,并启动语音识别,如果系统没有Activity处理请求的Intent,以Toast形式提示;
  • onActivityResult:获得语音识别后的结果并做相关处理;
  • onItemClick:ListView中数据项点击事件处理------显示;

运行效果如图:











多说一句:当然还有其它方法实现语音识别,但此方法比较简单!:)

更多相关文章

  1. android 浏览器全屏显示
  2. 命令行修改Android系统时间
  3. Android(安卓)Studio安装与使用最详细的图解教程
  4. Android(安卓)Studio设置
  5. 2011.09.14(3)——— android 自定义tabhost的tabs
  6. android布局文件中一些属性介绍
  7. Android客制化------在设置中加入RAM flash计算
  8. Android(安卓)几种弹框样式 自定义Dialog PopupWindow的使用
  9. Android(安卓)XML设置圆角边框

随机推荐

  1. Android一键锁屏源码
  2. android shape的使用
  3. Android自带Music播放器更新播放时间和进
  4. Android架构分析之使用自定义硬件抽象层(
  5. Android(安卓)adb安装apk时提示Invalid A
  6. Java編程和Android編程的區別
  7. Android(安卓)Studio GsonFormat插件的使
  8. Android(安卓)requires compiler complia
  9. 如何解决Eclipse开发android程序的编译错
  10. Android异步加载图片,并缓存到SD卡