(Android practice) Speech Recognition: performs operations based on speech

Source: Internet
Author: User

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 );
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.