我正在Android应用中实现语音识别。但是在我和我的手机通话后,应用程序向我展示了MainActivity(我从不同的活动开始语音识别Intent),以及Dialog女巫选项以选择赢,t出现。您可以在下面找到我的代码。
public class AddFoood extends Activity {
private static final int REQUEST_CODE = 1234;
Button voice;
Dialog match_text_dialog;
ArrayList<String> matches;
ListView textlist;
String SPEACH_RESULT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_foood);
getActionBar().setTitle("Add Food");
voice = (Button)findViewById(R.id.voiceButton);
voice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isConnected()){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
//RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 15);
startActivityForResult(intent,REQUEST_CODE);
}else {
Toast.makeText(getApplicationContext(),"No internet connection",Toast.LENGTH_SHORT).show();
}
}
});
}
public boolean isConnected()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = cm.getActiveNetworkInfo();
if (net!=null && net.isAvailable() && net.isConnected()) {
return true;
} else {
return false;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
match_text_dialog = new Dialog(AddFoood.this);
match_text_dialog.setContentView(R.layout.dialog_matches_frag);
match_text_dialog.setTitle("Select Matching Text");
textlist = (ListView)match_text_dialog.findViewById(R.id.list);
matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, matches);
textlist.setAdapter(adapter);
textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
SPEACH_RESULT = matches.get(position);
match_text_dialog.hide();
}
});
match_text_dialog.show();
}
}
答案 0 :(得分:0)
您无法使用RecognizerIntent.EXTRA_LANGUAGE_MODEL
您应该使用RecognizerIntent.EXTRA_LANGUAGE
代替。修改您的代码如下:
//Specify language
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
// Specify language model
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);