[代码]Android 轻松实现语音识别的完整代码

001

* Copyright (C)2008The Android Open Source Project

002

*


003

* Licensed under the Apache License, Version2.0(the"License");

004

* you may not usethisfile except in compliance with the License.


005

* You may obtain a copy of the License at

006

*


007

* http://www.apache.org/licenses/LICENSE-2.0

008

*


009

* Unless required by applicable law or agreed to in writing, software

010

* distributed under the License is distributed on an"AS IS"BASIS,


011

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

012

* See the Licenseforthe specific language governing permissions and


013

* limitations under the License.

014

*/


015

016

packagecom.example.android.apis.app;


017

018

importcom.example.android.apis.R;


019

020

importandroid.app.Activity;


021

importandroid.content.Intent;

022

importandroid.content.pm.PackageManager;


023

importandroid.content.pm.ResolveInfo;

024

importandroid.os.Bundle;


025

importandroid.speech.RecognizerIntent;

026

importandroid.view.View;


027

importandroid.view.View.OnClickListener;

028

importandroid.widget.ArrayAdapter;


029

importandroid.widget.Button;

030

importandroid.widget.ListView;


031

032

importjava.util.ArrayList;


033

importjava.util.List;

034


035

/**

036

* Sample code that invokes the speech recognition intent API.


037

*/

038

publicclassVoiceRecognitionextendsActivityimplementsOnClickListener {


039

040

privatestaticfinalintVOICE_RECOGNITION_REQUEST_CODE =1234;


041

042

privateListView mList;


043

044

/**


045

* Called with the activity is first created.

046

*/


047

@Override

048

publicvoidonCreate(Bundle savedInstanceState) {


049

super.onCreate(savedInstanceState);

050


051

// Inflate our UI from its XML layout description.

052

setContentView(R.layout.voice_recognition);


053

054

// Get display items for later interaction


055

Button speakButton = (Button) findViewById(R.id.btn_speak);

056


057

mList = (ListView) findViewById(R.id.list);

058


059

// Check to see if a recognition activity is present

060

PackageManager pm = getPackageManager();


061

List activities = pm.queryIntentActivities(

062

newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);


063

if(activities.size() !=0) {

064

speakButton.setOnClickListener(this);


065

}else{

066

speakButton.setEnabled(false);


067

speakButton.setText("Recognizer not present");

068

}


069

}

070


071

/**

072

* Handle the click on the start recognition button.


073

*/

074

publicvoidonClick(View v) {


075

if(v.getId() == R.id.btn_speak) {

076

startVoiceRecognitionActivity();


077

}

078

}


079

080

/**


081

* Fire an intent to start the speech recognition activity.

082

*/


083

privatevoidstartVoiceRecognitionActivity() {

084

Intent intent =newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);


085

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,

086

RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);


087

intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speech recognition demo");

088

startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);


089

}

090


091

/**

092

* Handle the results from the recognition activity.


093

*/

094

@Override


095

protectedvoidonActivityResult(intrequestCode,intresultCode, Intent data) {

096

if(requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {


097

// Fill the list view with the strings the recognizer thought it could have heard

098

ArrayList matches = data.getStringArrayListExtra(


099

RecognizerIntent.EXTRA_RESULTS);

100

mList.setAdapter(newArrayAdapter(this, android.R.layout.simple_list_item_1,


101

matches));

102

}


103

104

super.onActivityResult(requestCode, resultCode, data);


105

}

106

}

更多相关文章

  1. android 系统关机,重启
  2. Android(安卓)BLE Gatt实现原理解析(未完)
  3. android 自定义ListView中的单项选择
  4. Android桌面小部件实例 桌面小时钟
  5. android bitmap 处理
  6. 仿UC底部菜单栏实例源码
  7. Android(安卓)实现 按钮从两边移到中间动画效果
  8. android手机两种方式获取IP地址
  9. 在下方的tab页

随机推荐

  1. Android(安卓)ion异步网络和图像加载大大
  2. Android(安卓)用HttpClient 以Post方式提
  3. Android获取全局Context的方法
  4. Android(安卓)adt-bundle开发环境搭建小
  5. java.lang.IllegalAccessError: Class re
  6. Win8 专业版安装Android(安卓)Studio
  7. Android保持屏幕常亮唤醒状态的方法
  8. android gallery3d 源码分析(二)
  9. android调用其他人的so文件
  10. android,做一个定制化的TextView,在TextVie