Android uses recognizerintent to recognize speech and make phone calls simple

Source: Internet
Author: User

About Android using Recognizerintent to recognize the voice and simply implement the call, see the implementation code in detail such as the following:
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 VOI Ce_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 {//pass through Intent speech recognition mode, turn on voice intent intent = new Intent (Recognizerinte Nt. Action_recognize_speech)///language mode and Free mode speech recognition Intent.putextra (recognizerintent.extra_language_modEl,recognizerintent.language_model_free_form);//Prompts The voice to start Intent.putextra (recognizerintent.extra_prompt, "Start Voice");// Start speech recognition Startactivityforresult (Intent,voice_recognition_request_code);} catch (Exception e) {//Todo:handle exceptione.printstacktrace (); Toast.maketext (Getapplicationcontext (), "Voice device not Found", 1). Show ();}});} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stub Callback gets data obtained from Google if (Requestcode = = voice_recognition_request_code&& ResultCode = = RESULT_OK) {// Gets the phonetic character 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 ("Hello") >-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,"No call to call", 1). Show ();}} Super.onactivityresult (Requestcode, ResultCode, data);}}
Here, we need to be aware of the need to use two of permissions. One is the network resource. One is the call of this action permission, so we need to change androidmainfest.xml add two items. For example, the following
<uses-permission android:name= "Android.permission.CALL_PHONE"/><uses-permission android:name= " Android.permission.INTERNET "/>

Android uses recognizerintent to recognize speech and make phone calls simple

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.