Requirements: The project needs to add a voice search module to increase the user experience
Solve the process: Search the Internet for voice searching examples, refer to the online code, add to their own projects to complete the product requirements. This problem is very good to solve, the Internet can find a lot of information, but there is no direct import project can use the example, I wrote here a complete demo, the code can be pasted directly into their projects to achieve
Speech search, and display the search results.
Speech Search General Process: Start the system with the Intent,intent parameter set to Recognizerintent.action_recognize_speech, plus some hint parameters
Intent.putextra (Recognizerintent.extra_language_model, recognizerintent.language_model_free_form);
When I finished the speech search in the Onactivityresult to receive data, I just show the first results of voice search, the system provides a lot of results, according to the similarity arrangement. I used the handler in the demo and showed the data in the handler. In the case of the project, some processing of the data is done in handler. During the development process, some speech search crashes have been encountered, and it is found that when the results are displayed, do not do some time-consuming operations.
The code is as follows
public class Mainactivity extends activity {private Button btnvoice;
Private TextView Tvvoiceresult;
private static final int voice_recognition_request_code = 123456; Private Handler Jumphandler = new Handler () {public void Handlemessage (Android.os.Message msg) {Tvvoiceresul
T.settext ((String) msg.obj);
};
};
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Btnvoice = (Button) Findviewbyid (R.id.btnvoice);
Tvvoiceresult = (TextView) Findviewbyid (R.id.tvvoiceresult);
Btnvoice.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {
try {Intent Intent = new Intent (Recognizerintent.action_recognize_speech);
Intent.putextra (Recognizerintent.extra_language_model, recognizerintent.language_model_free_form); Intent.putextra (Recognizerintent.extra_PROMPT, "Please start talking");
Startactivityforresult (Intent, Voice_recognition_request_code);
}catch (activitynotfoundexception e) {alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this);
Builder.settitle ("speech recognition"); Builder.setmessage ("Your phone does not support voice search function, click OK download to install Google Voice search software." You can also search for "voice search" for download installations at each App store.
"); Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void on
Click (dialoginterface dialog, int which) {//Jump to download Voice page}});
Builder.setnegativebutton ("Cancel", null);
Builder.show ();
}
}
}); @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.activity_main, M
ENU);
return true; @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityr Esult (Requestcode, ResultCode, data); if (Requestcode = = Voice_recognition_request_code && ResultCode = mainactivity.result_ok) {arraylist<s
tring> matches = Data.getstringarraylistextra (recognizerintent.extra_results);
if (matches.size () > 0) {msg = new message ();
Msg.obj = matches.get (0);
Jumphandler.sendmessage (msg); }
}
}
}
The above is a small example of the development of the Android speech module, a friend in need can refer to it.