Android利用RecognizerIntent识别语音并简单实现打电话动作

【Android利用RecognizerIntent识别语音并简单实现打电话动作】少年意气强不羁,虎胁插翼白日飞。这篇文章主要讲述Android利用RecognizerIntent识别语音并简单实现打电话动作相关的知识,希望能为你提供帮助。
关于android利用RecognizerIntent识别语音并简单实现打电话,详细看实现代码例如以下:

package com.example.recognizerintentactivity; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.speech.RecognizerIntent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity {private Button btnReconizer; private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnReconizer = (Button) this.findViewById(R.id.btnRecognizer); btnReconizer.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) { // TODO Auto-generated method stub try { // 通过Intent传递语音识别的模式,开启语音 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) { // TODO: handle exception e.printStackTrace(); Toast.makeText(getApplicationContext(), "找不到语音设备", 1) .show(); } } }); }@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub // 回调获取从谷歌得到的数据 if (requestCode == VOICE_RECOGNITION_REQUEST_CODE & & resultCode == RESULT_OK) { // 取得语音的字符 ArrayList< String> results = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); String resultString = ""; for (int i = 0; i < results.size(); i++) { resultString += results.get(i); }if(resultString.indexOf("喂") > -1){ String tel = "15100000000"; Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + tel)); startActivity(intent); Toast.makeText(this,tel, 1).show(); }else{ Toast.makeText(this, "没找到要拨打的电话", 1).show(); } } super.onActivityResult(requestCode, resultCode, data); } }

这里,我们须要注意的是,须要用到两个权限。一个是网络资源。一个是打电话的这个动作权限,因此,我们须要改动 AndroidMainfest.xml添加两条内容。例如以下
< uses-permission android:name="android.permission.CALL_PHONE" /> < uses-permission android:name="android.permission.INTERNET" />


    推荐阅读