Android speech recognition function usage

Source: Internet
Author: User
Package com. king. android. speical; import java. util. arrayList; import com. king. android. r; import android. app. activity; import android. content. activityNotFoundException; import android. content. intent; 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;/*** Description: speech recognition function (only tested on a real machine) * by Andy. liu * Time: 07:38:27 **/public class VoiceActivity extends Activity implements OnClickListener {private static final int listener = 4321; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Button btnVoice = (Button) findViewById (R. id. btn_voice); btnVoice. setVisibility (View. VISIBLE); btnVoice. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_voice: doStartVoice () ;}}/***** TODO: Start speech * Author: Andy. liu * Create Time: 07:42:48 * TAG: * Return: void */private void doStartVoice () {try {// The Speech Recognition mode transmitted through intent, intent intent = new Intent (RecognizerIntent. ACTION_RECOGNIZE_SPEECH); // language pattern and free-form idea recognition intent. putExtra (RecognizerIntent. EXTRA_LANGUAGE_MODEL, RecognizerIntent. LANGUAGE_MODEL_FREE_FORM); // indicates that the voice starts intent. putExtra (RecognizerIntent. EXTRA_PROMPT, "Start voice"); startActivityForResult (intent, VOICE_RECOGNITION_REQUEST_CODE);} catch (ActivityNotFoundException e) {// find the Toast Voice device. makeText (VoiceActivity. this, "no device found", Toast. LENGTH_LONG ). show () ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (VOICE_RECOGNITION_REQUEST_CODE = requestCode & RESULT_ OK = resultCode) {ArrayList <String> results = data. getStringArrayListExtra (RecognizerIntent. EXTRA_RESULTS); StringBuilder sb = new StringBuilder (); for (String voice: results) {sb. append (voice);} Toast. makeText (VoiceActivity. this, sb. toString (), Toast. LENGTH_LONG ). show ();} super. onActivityResult (requestCode, resultCode, data );}}
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.