我正在构建一个应用程序,该应用程序将提示用户说几个单词,然后将单词与字符串进行比较,并根据所说的字符串打开新活动。 我正在使用的代码是:
package com.example.voiceex;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
static final int check=1111;
static String res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something");
startActivityForResult(i, check);
}
});
/*if("hello".equalsIgnoreCase(res))
{
Toast.makeText(getBaseContext(), "Hello is spoken", Toast.LENGTH_SHORT).show();
}
else if("i am hungry".equalsIgnoreCase(res))
{
Toast.makeText(getBaseContext(), "Ill give you some food", Toast.LENGTH_LONG).show();
}*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onActivityResult(int request,int result,Intent data)
{
if(request==check&&result==RESULT_OK)
{
res=data.getStringExtra(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
Toast.makeText(getBaseContext(), res, Toast.LENGTH_SHORT).show();
}
}
}
这里的吐司是空白的。即使使用了评论部分,也没有任何反应。 我哪里错了? 感谢
答案 0 :(得分:1)
RecognizerIntent返回匹配数组,您应该确定自己要使用哪个字符串,通常它是第一个表单数组
ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); for(String match : matches){ Log.i("Matched text", match); }