使用语音识别拨号

时间:2014-07-20 19:10:16

标签: android

我正在尝试创建一个应用程序,它从语音中拨打号码并拨打号码......以下是我的代码..

public class MainActivity extends Activity{
static int check = 1234;
Button speak_btn,Dial_btn;
TextView speechText;
ArrayList<String> results;

@Override

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 

speak_btn = (Button)findViewById(R.id.button_Speak);

speechText = (TextView)findViewById(R.id.SpeechtextView);//Shows the Number to be Dialed

Dial_btn = (Button)findViewById(R.id.button_dial);


speak_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
startVoiceRecognitionActivity();    
}
});


  Dial_btn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:"+speechText));
    callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(callIntent);


  }
  });

  }

  public void startVoiceRecognitionActivity(){

Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
i.putExtra(RecognizerIntent.EXTRA_PROMPT,"Say A Phone Number!");
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
 startActivityForResult(i, check);
 }

 @Override

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub

if(requestCode==check && resultCode==RESULT_OK){

  results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

 speechText.setText(results.get(0));

}


    super.onActivityResult(requestCode, resultCode, data);
 }  
 }

问题是......每次按下拨号键,它都会拨打相同的号码(&#34; 2637643&#34;)。(即使我不说任何号码)。我不会#39 ;了解出了什么问题

1 个答案:

答案 0 :(得分:0)

您的问题在以下一行:

 callIntent.setData(Uri.parse("tel:" + speechText));

应该是:

 callIntent.setData(Uri.parse("tel:" + speechText.getText().toString()));

即使您不说话,您仍然可以获得号码的原因是因为"tel:" + speechText导致toString()方法在speechText上被调用,这是{{1}并且,它返回Object引用的表示法,而不是它包含的文本。