Why is the ACTION_CHECK_TTS_DATA Intent “awkward to use”?

我怕爱的太早我们不能终老 提交于 2019-11-26 21:41:37

问题


The official introduction to Text-To-Speech in Android says that "upon creating your activity, a good first step is to check for the presence of the TTS resources with the corresponding intent:"

Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

But @gregm in this thread suggests otherwise:

Also, don't use the ACTION_CHECK_TTS_DATA Intent, that's awkward to use.

Instead, do the following:

  1. Create TextToSpeech
  2. OnInit, check isLanguageAvailable() if it is, your app is all set. if not, send the ACTION_INSTALL_TTS_DATA

If I understand correctly, what @gregm does/suggests is to defer the TextToSpeech.LANG_MISSING_DATA check from onActivityResult() to onInit().

Why is this better than the formal approach?

And why is ACTION_CHECK_TTS_DATA so "awkward to use"?


回答1:


This is @gregm.

ACTION_CHECK_TTS_DATA requires more complicated execution and more code, but yet does the same thing as TextToSpeech.isLanguageAvailable() Let me explain:

1) You need TWO asynchronous processes when you use ACTION_INSTALL_TTS_DATA. First, to launch an Intent and receive the result. Second, to wait for TextToSpeech to call onInit() More can go wrong while your app is doing all that waiting.

2) All you are trying to do is execute an if statement and you need the same code to handle installing the language data if need be, so why bother adding extra complexity?

It boils down do this:

Do you want 1 line of code:

if (TextToSpeech.isLanguageAvailable())
{
 (same tts init code here)
}

or >1 lines of code like:

Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

public void onActivityResult(...)
{
 if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
 (same tts init code here)
}

In my point of view, I'd rather just get to the if statement, instead of using an Intent runaround. I believe the official Android documentation needs to change to recommend this approach.

If you still don't believe that both approaches we are discussing do the same exact thing, check out the alternative implementations I have here and here. Also, If you don't care about all this stuff and just want your app to speak, just extend this Activity and be done.



来源:https://stackoverflow.com/questions/11550746/why-is-the-action-check-tts-data-intent-awkward-to-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!