標籤:Google lang 傳遞 amp toast private inf mis ted
關於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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnReconizer = (Button) this.findViewById(R.id.btnRecognizer);btnReconizer.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry {// 通過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 exceptione.printStackTrace();Toast.makeText(getApplicationContext(), "找不到語音裝置", 1).show();}}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stub// 回調擷取從Google得到的資料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" />
Android利用RecognizerIntent識別語音並簡單實現打電話動作