我有一個菜單活動,有幾個選項。其中之一是使用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);
}
你認爲你可以給如何做到這一點的例子嗎?我不完全確定如何做到這一點。 – Natalie
我編輯了我的答案以包含示例。希望能幫助到你! –