2

여러 가지 옵션이있는 메뉴 활동이 있습니다. 그 중 하나는 Glass의 음성 텍스트를 사용하여 뉴스 피드에 게시하는 것입니다. 나는 https://developers.google.com/glass/develop/gdk/input/voice#starting_the_speech_recognition_activity 을 보았고 모두 구현했지만 onActivityResult 메서드는 결코 호출되지 않습니다.음성에 대해 onActivityResult가 호출되지 않았습니다. Google Glass의 의도

글래스 장치에서 메뉴에서 "새 글쓰기"를 선택할 수 있으며 음성 캡처가 나타납니다. 내가 말할 수 있고 내 연설을 화면의 텍스트로 바꿀 것입니다. 그러나 (수초를 두드 리거나 기다리는 것으로) 내가 그것을 받아들이면, 그냥 나가서 홈 화면으로 돌아 가게됩니다. onActivityResult에서 음성 텍스트 문자열을 가져올 수 있어야하고 다른 메서드 (displayPostMenu)를 호출하여 텍스트를 처리하는 다른 메뉴를 표시 할 수 있어야하지만 onActivityResult가 호출되지 않으면이를 수행 할 수 없습니다.

몇 가지 유사한 문제를 살펴 보았지만 해결 방법이 없었거나 적용 가능하지 않았습니다 ... RecognizerIntent.ACTION_RECOGNIZE_SPEECH에서 setResult()를 사용할 수 있다고 생각하지 않습니다. 어떤 도움이라도 대단히 감사하겠습니다! 내 코드의

일부 조각 :

private final int SPEECH_REQUEST = 1; 

//Code to make this Activity work and the menu open... 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.view_feed: 
     //Stuff 
     return true; 
    case R.id.new_post: 
     Log.i("MainMenu", "Selected new_post"); 
     displaySpeechRecognizer(); 
     Log.i("MainMenu", "Ran displaySpeechRecog under new_post selection"); 
     return true; 
    case R.id.stop: 
     Activity parent = getParent(); 
     Log.i("MainMenu", "Closing activity; parent: " + parent + "; " + hashCode()); 
     if (parent != null && parent.getApplication() == getApplication()) { 
      finish(); 
     } else { 
      MainMenu.close(); 
     } 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

@Override 
public void onOptionsMenuClosed(Menu menu) { 
    // Nothing else to do, closing the Activity. 
    finish(); 
} 

public void displaySpeechRecognizer() { 
    Log.i("MainMenu", "Entered displaySpeechRecognizer"); 
    Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    startActivityForResult(speechIntent, SPEECH_REQUEST); 
    Log.i("MainMenu", "Finished displaySpeechRecognizer. startActivityForResult called."); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Log.i("MainMenu", "onActivityResult entered from MainMenu"); 
    switch (requestCode) { 
    case SPEECH_REQUEST: 
     Log.i("MainMenu", "onActivityResult enters SPEECH_REQUEST case"); 
     if (resultCode == RESULT_OK) { 
      Log.i("MainMenu", "onActivityResult enters RESULT_OK for voice cap"); 
      List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      String spokenText = results.get(0); 
      Log.i("MainMenu", "SpokenText:" + spokenText); 
      holdText = spokenText; 
      if (holdText != "") { 
       displayPostMenu(); 
      } 
     } 
    super.onActivityResult(requestCode, resultCode, data); 
    } 

답변

4

당신은 그래서 그것이 라이브 카드에 붙어 있음을 의미 하는가, 이것은 "메뉴의 활동"이라고?

그렇다면 onOptionsMenuClosed을 무시하고 finish을 호출하고 있습니까?

당신이 있다면, 활동이 끝나기 전에 메뉴 활동이 끝나고 파괴되므로 결과가 돌아갈 자리가 없습니다.

이 문제를 해결하기위한 한 가지 방법은 메뉴가 닫힐 때 finish에 대한 호출을 연기해야하는지 여부를 나타내는 플래그를 사용하고 해당 플래그를 기반으로 조건부로 onOptionsMenuClosed 내에서 해당 호출을 수행하는 것입니다. 그런 다음 해당 플래그를 displaySpeechRecognizer 방법으로 설정하고 onActivityResult 처리가 끝날 때까지 기다려 finish (으)로 전화하십시오. 이 같은

뭔가 (오타를 포함 할 수 있습니다 테스트없이 작성) 작동합니다 :

private boolean shouldFinishOnMenuClose; 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // By default, finish the activity when the menu is closed. 

    shouldFinishOnMenuClose = true; 

    // ... the rest of your code 
} 

private void displaySpeechRecognizer() { 
    // Clear the flag so that the activity isn't finished when the menu is 
    // closed because it will close when the speech recognizer appears and 
    // there won't be an activity to send the result back to. 

    shouldFinishOnMenuClose = false; 

    // ... the rest of your code 
} 

@Override 
public void onOptionsMenuClosed(Menu menu) { 
    super.onOptionsMenuClosed(); 

    if (shouldFinishOnMenuClose) { 
     finish(); 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == SPEECH_REQUEST) { 
     if (resultCode == RESULT_OK) { 
      // process the speech 
     } 

     // *Now* it's safe to finish the activity. Note that we do this 
     // whether the resultCode is OK or something else (so the menu 
     // activity goes away even if the user swipes down to cancel 
     // the speech recognizer). 

     finish(); 
    } 
} 
+0

당신이 그 작업을 수행하는 방법의 예를 줄 수 있다고 생각합니까? 나는 그것을하는 방법을 완전히 모르겠다. – Natalie

+0

예를 들어 답을 편집했습니다. 희망이 도움이됩니다! –

관련 문제