1

나는 음성 인식 기능을 추가해야하는 완전히 기능적인 맞춤형 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); 
} 

:

enter image description here

이 이상적으로 같은 달성 것 : 여기 트위스트는 uset 마이크 키를 탭하면 내가 구글 키보드에서 발생하는 유사한 행동을 할 것입니다 , 키 수신기가 켜져 있고 InputMethodService가 startActivityForResult를 호출 할 수 없기 때문입니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까? 레이아웃없이 새 액티비티를 시작하고 inputMethodService에 대한 콜백을 수행해야합니까? 지저분 해 보인다

+0

로 다시 떨어질 것이다 어떻게 그 일을 볼 수 있습니까? 그 방법을 기억하기에는 너무나 오랜 세월이 걸렸지 만 우리가 Swype을 위해 그것을했을 때 복사했습니다. –

+0

감사합니다. LatinIME과 im이 실제로 잘못된 접근법을 사용하고 있는지 확인했습니다. 나는 잠시 후에 대답을 게시 할 것이다. – Adr3nl

답변

2

스크린 샷은 마이크 버튼을 눌렀을 때 Google 키보드에서 호출하는 독립적 인 IME 인 "Google voice typing"을 보여줍니다. 따라서 IME는 동일한 작업을 수행해야합니다. 음성 입력을 제공하는 IME로 대체하고 음성 입력이 완료되면 IME에 대한 백 링크가 있기를 바랍니다.

가장 간단한 구현은 Switching among IME Subtypes이지만 더 많은 제어가 필요할 수 있습니다. 특정 입력 매개 변수 등으로 특정 IME를 시작하십시오.이 여분의 제어를 달성하기위한 최상의/표준 방법이 무엇인지 확실하지 않습니다.

음성 입력 IME의 예를 들어 (내 앱) Kõnele을 살펴볼 수 있습니다.

솔루션의
+0

나는 똑같은 답변을 게시하려하고있었습니다! 예, 접근 방식은 현재 IME를 Google 음성으로 트리거하는 것입니다. 일단 나는 그것을 완벽하게 말한 밖으로 발견 – Adr3nl

1

간단한 구현 : 우리가 더 이상 현재의 IME를 사용하려면 일단

// on mic tap we call 
public void startVoiceListening() { 
    InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE); 
    String voiceExists = voiceExists(imeManager); 
    if (voiceExists != null) { 
     final IBinder token = getWindow().getWindow().getAttributes().token; 
     imeManager.setInputMethod(token,voiceExists); 
    } 
} 

private String voiceExists(InputMethodManager imeManager) { 
    List<InputMethodInfo> list = imeManager.getInputMethodList(); 
    for (InputMethodInfo el : list) { 
     // do something to check whatever IME we want. 
     // in this case "com.google.android.googlequicksearchbox" 
    } 
    return null; 
} 

그냥 닫습니다하지 않으며 그것은 당신이 구글의 LatinIME 키보드 봤어 이전