onActivityResult never called for speech Intent on Google Glass
我有一个带有多个选项的菜单 Activity。其中之一是使用 Glass 的语音到文本发布到新闻提要。我查看了 https://developers.google.com/glass/develop/gdk/input/voice#starting_the_speech_recognition_activity
并全部实现,但 onActivityResult 方法显然永远不会被调用。
在 Glass 设备上,我可以在菜单中选择"新帖子",然后会出现语音捕获。我可以和它说话,它会将我的语音转换为屏幕上的文本。但是在我接受它之后(通过点击或等待几秒钟),它就会退出并带我回到主屏幕。我需要能够在 onActivityResult 中获取语音文本字符串并调用另一个方法(displayPostMenu)来显示另一个菜单来处理文本,但如果从不调用 onActivityResult,我就不能这样做。
我已经查看了几个类似的问题,但没有一个解决方案有效/不适用...我认为我不能在 RecognizerIntent.ACTION_RECOGNIZE_SPEECH 上设置结果(),因为它是 Google 的?任何帮助将不胜感激!
我的一些代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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); } |
你说这是一个"菜单活动",那么这是否意味着它是附加在一张实时卡片上的?
如果是这样,你是否覆盖
如果是,菜单活动将在语音活动返回之前完成并自行销毁,因此结果将无处可返回。
解决此问题的一种方法是使用一个标志来指示您是否应该在菜单关闭时推迟对
这样的东西应该可以工作(未经测试编写,可能包含拼写错误):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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(); } } |