Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java语音识别输入屏幕与安卓中的所有输入屏幕相同

我的安卓应用程序有语音识别问题。我的问题是,我想显示与jellybean键盘相同的语音识别输入屏幕。如果我从代码中调用语音识别意图,它不会继续监听,并且会过早停止。 一定有个简单的方法,但我找不到

这是我的密码

package com.bsdcservices.recognitiontest;

import java.util.ArrayList;
import java.util.Locale;

import 安卓.os.Bundle;
import 安卓.app.Activity;
import 安卓.content.ActivityNotFoundException;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.speech.RecognizerIntent;
import 安卓.view.Menu;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.view.inputmethod.InputMethodManager;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.Toast;

public class MainActivity extends Activity {

private final String TAG = "RecognitionTest";
private final int SPEECHRECOGNITION_RESULTCODE = 0;
private EditText editText;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.textbox);
    Button recognize = (Button) findViewById(R.id.recognize);
    recognize.setOnClickListener(buttonClickListener);
}

public OnClickListener buttonClickListener = new OnClickListener(){

    @Override
    public void onClick(View v) {
        startSpeechRecognitionActivity();
    }

};

@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;
}

void startSpeechRecognitionActivity(){
    //      MyApplication.speechRecognitionActive = true;
    //      main.startSpeechRecognizer(TABSPEAK);
    try{
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().toString());
        startActivityForResult(intent, SPEECHRECOGNITION_RESULTCODE);
    } catch (ActivityNotFoundException error) {
        Toast.makeText(getBaseContext(), "Speech recognition is not supported by your device" , Toast.LENGTH_LONG).show();
    } catch( RuntimeException error ) {
        Toast.makeText(getBaseContext(), error.getMessage() , Toast.LENGTH_LONG).show();
        error.printStackTrace();
    }
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try
    {
        if (requestCode == SPEECHRECOGNITION_RESULTCODE) {
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                editText.append(text.get(0));
            }
        }
    } catch( RuntimeException error ) {
        Toast.makeText(getBaseContext(), error.getMessage() , Toast.LENGTH_LONG).show();
        error.printStackTrace();
    }
}

}


共 (0) 个答案