在iphone4s 發布後,siri語音功能成為了一時成為了熱點,這幾天想寫個類似於siri類似功能的android應用,下面就是關鍵的兩個技術點
1 語音辨識:
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
/**
* 開啟語音辨識對話表單
*/
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);
}
/**
* 處理語音對話方塊返回的識別資訊.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
&& resultCode == RESULT_OK) {
// 擷取反饋的語音辨識數組,並按照匹配度反饋
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
}
}
2 根據指令,進行打電話,發簡訊,開啟網站操作:
if (voicekey.type == 1) { //電話
txtview_secretary.setText("正在為你接通電話...");
Thread.sleep(1000);
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("tel://" + voicekey.data));
startActivity(intent);
} else if(voicekey.type == 2){//簡訊
txtview_secretary.setText("正在為你發簡訊...");
Thread.sleep(1000);
Intent intent = new Intent(Intent.ACTION_SENDTO,Uri.parse("sms:"+voicekey.data));
startActivity(intent);
}
else if(voicekey.type == 3) //網址
{
// open kit explore ,navigate to net bank,close App
txtview_secretary.setText("正在為你開啟網站...");
Thread.sleep(1000);
Uri uri = Uri.parse(voicekey.data);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}