android 语音识别

1.先检测devices是否support.

PackageManager pm = getPackageManager();
		List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

		if (activities.size() == 0) {
			btnSpeak.setEnabled(false);
			btnSpeak.setText("Voice recognize disabled");
		}

 2.发送intent。

   

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, "start");
				startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				Toast.makeText(RecognizeCall.this, "Not found voice recognize devices!", Toast.LENGTH_LONG).show();
			}

 3.获取返回的content:

   

@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
			ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			String result="";
			for (String temp : results) {
				result += temp + "\r\n";
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

 权限:

    

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>

猜你喜欢

转载自bgj.iteye.com/blog/1968031