1,判断是否支持语音识别
p_w_picpathbtn = (ImageButton)findViewById(R.id.p_w_picpathBtn01); p_w_picpathbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub 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, "请说话,我识别");//出现语言识别界面上面需要显示的提示 startActivityForResult(intent,REQUEST_CODE); } catch(ActivityNotFoundException e){ e.printStackTrace(); Toast.makeText(MianActivity.this , "找不到语音设备", Toast.LENGTH_SHORT).show(); } } });
2,启动语音识别
ArrayList<String> results= data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); //listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list)); //把数据显示在listview中 String resultsString = ""; resultsString =resultsString+results.get(0);
3,获取识别结果,并以Toast显示出来
Toast.makeText(MianActivity.this, resultsString, Toast.LENGTH_SHORT).show(); super.onActivityResult(requestCode, resultCode,data);