package com.example.wenandroid;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;public class VoiceRecognizeDemo extends Activity implements OnClickListener {private static final int VOICE_RECOGNITION_REQUEST_CODE=1234;private ListView listview;private Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.voicerecogenize);listview=(ListView)findViewById(R.id.listview);btn=(Button)findViewById(R.id.btn);PackageManager pm=getPackageManager();List<ResolveInfo> activities = pm.queryIntentActivities(            new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);    if (activities.size() != 0) {        btn.setOnClickListener(this);    } else {        btn.setEnabled(false);        btn.setText("Recognizer not present");    }}@Overridepublic void onClick(View v) {if(v.getId()==R.id.btn){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);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {            // Fill the list view with the strings the recognizer thought it could have heard            ArrayList<String> matches = data.getStringArrayListExtra(                    RecognizerIntent.EXTRA_RESULTS);            listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,                    matches));        }super.onActivityResult(requestCode, resultCode, data);}}


界面只有一个listview和一个button。

更多相关文章

  1. Android中隐藏标题栏和状态栏
  2. viewPager的简单实现
  3. Android(安卓)android:focusableInTouchMode
  4. Android(安卓)访问网络连接设置界面
  5. android 界面属性
  6. android gallery笔记
  7. android如何用代码实现界面ui
  8. Android一款界面良好使用简单的弹出框
  9. [CSDN]Android应用程序启动过程源代码分析

随机推荐

  1. android 主线程和子线程之间的消息传递
  2. How to destroy an Activity in android
  3. Android(安卓)Studio 1.5.1最新版下载地
  4. Android(安卓)Material Design : Collaps
  5. android 中让activity全屏幕显示
  6. android之针对fragment多次调用onCreateV
  7. Android应用程序键盘(Keyboard)消息处理机
  8. retrofit2+RxJava简单使用
  9. Android(安卓)library projects cannot b
  10. android 判断网络时打开设置