나는 음성 인식 기능을 추가해야하는 완전히 기능적인 맞춤형 Android 키보드가 있습니다. 여기에 구현의 관련 부분은 내가SpeechRecognizer가있는 Android 맞춤 키보드
public class CustomInputMethodService
extends InputMethodService
implements <random stuff> {
private SpeechRecognizer mSpeechRecognizer;
private RecognitionListener mSpeechlistener;
public void onCreate() {
super.onCreate();
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechlistener = new CustomRecognitionListener();
mSpeechRecognizer.setRecognitionListener(mSpeechlistener);
}
@Override
public void onPress(int primaryCode) {
if (primaryCode == KeyCodes.VOICE_INPUT) {
mSpeechRecognizer.startListening(getSpeechIntent());
}else if(..){
...
}
}
private Intent getSpeechIntent() {
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
return speechIntent;
}
}
이있는 CustomRecognitionListener의 관련 방법은 간단하다 :이 코드는 잘 작동하고
@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.d(TAG, "onResults: ----> " + matches.get(0));
if(matches != null && matches.size() > 0) {
writeText(matches.get(0));
}
}
. 그러나
Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
try {
startActivityForResult(voiceIntent, Constants.RESULT_SPEECH);
} catch (ActivityNotFoundException ex) {
DebugLog.e(TAG, "Not found excpetion onKeyDown: " + ex);
}
:
이 이상적으로 같은 달성 것 : 여기 트위스트는 uset 마이크 키를 탭하면 내가 구글 키보드에서 발생하는 유사한 행동을 할 것입니다 , 키 수신기가 켜져 있고 InputMethodService가 startActivityForResult를 호출 할 수 없기 때문입니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까? 레이아웃없이 새 액티비티를 시작하고 inputMethodService에 대한 콜백을 수행해야합니까? 지저분 해 보인다
로 다시 떨어질 것이다 어떻게 그 일을 볼 수 있습니까? 그 방법을 기억하기에는 너무나 오랜 세월이 걸렸지 만 우리가 Swype을 위해 그것을했을 때 복사했습니다. –
감사합니다. LatinIME과 im이 실제로 잘못된 접근법을 사용하고 있는지 확인했습니다. 나는 잠시 후에 대답을 게시 할 것이다. – Adr3nl