After the release of iPhone 4S, siri voice functions have become a hot topic for the moment. In the past few days, I want to write an android Application similar to siri. The following are two key technical points:
1. Speech Recognition:
Private static final int voice_recognition_request_codes = 1234;
/**
* Enable the speech recognition dialog form
*/
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 );
}
/**
* Process the recognition information returned by the voice dialog box.
*/
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
If (requestCode = VOICE_RECOGNITION_REQUEST_CODE
& ResultCode = RESULT_ OK ){
// Obtain the feedback Speech Recognition array and feedback based on matching degree
ArrayList <String> matches = data
. GetStringArrayListExtra (RecognizerIntent. EXTRA_RESULTS );
}
}
2. Call, send text messages, and open the website according to the instructions:
If (voicekey. type = 1) {// phone number
Txtview_secretary.setText ("connecting your phone ...");
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) {// SMS
Txtview_secretary.setText ("you are sending a text message ...");
Thread. sleep (1000 );
Intent intent = new Intent (Intent. ACTION_SENDTO, Uri. parse ("sms:" + voicekey. data ));
StartActivity (intent );
}
Else if (voicekey. type = 3) // URL
{
// Open kit release E, navigate to net bank, close App
Txtview_secretary.setText ("opening your website ...");
Thread. sleep (1000 );
Uri uri = Uri. parse (voicekey. data );
Intent intent = new Intent (Intent. ACTION_VIEW, uri );
StartActivity (intent );
}