【问题标题】:Android speech recognition result dialog won't show upAndroid 语音识别结果对话框不会出现
【发布时间】:2014-12-15 17:30:56
【问题描述】:

我正在 Android 应用中实现语音识别。但是在我与手机通话后,应用程序会显示 MainActivity(我正在从不同的活动开始语音识别 Intent),并且选择不会显示的对话框女巫选项。您可以在下面找到我的代码。

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();
    }

}

【问题讨论】:

    标签: java android android-studio speech-recognition


    【解决方案1】:

    您不能使用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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多