这个代码是我在网上拷的,我运行的时候一直提示找不到设备,请各位大侠帮忙看看package com.listen;import android.app.Activity;
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;public class listenActivity extends Activity {
/** Called when the activity is first created. */
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
//通过Intent传递语音识别的模式,开启语音
Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//语言模式和自由模式的语音识别
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//提示语音开始
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音");
//开始语音识别
System.out.println("开始语音");
startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);
System.out.println("执行完毕");
}catch(Exception ex){
ex.printStackTrace();
Toast.makeText(getApplicationContext(), "找不到语音设备", 1).show();
}
}
});
}
}
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;public class listenActivity extends Activity {
/** Called when the activity is first created. */
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
//通过Intent传递语音识别的模式,开启语音
Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//语言模式和自由模式的语音识别
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//提示语音开始
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音");
//开始语音识别
System.out.println("开始语音");
startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);
System.out.println("执行完毕");
}catch(Exception ex){
ex.printStackTrace();
Toast.makeText(getApplicationContext(), "找不到语音设备", 1).show();
}
}
});
}
}
List<ResolveInfo> list = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if(list.size() == 0) {
btn.setText("当前语音识别设备不可用...");
}
楼主在发送那个意图前,先加上这个逻辑,看看是不是你手机不支持语音识别。