2

我有一個菜單活動,有幾個選項。其中之一是使用Glass的語音到文本發佈到新聞Feed。我看着https://developers.google.com/glass/develop/gdk/input/voice#starting_the_speech_recognition_activity 並且已經全部實現了,但onActivityResult方法顯然永遠不會被調用。onActivityResult從來沒有要求在Google Glass上發言意圖

在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

我編輯了我的答案以包含示例。希望能幫助到你! –

相關問題