简体   繁体   English

执行方法之前的延迟

[英]Delay before executing method

I start an activity from another activity with an intent. 我从另一个活动开始一个活动。 This second activity doesn't have any UI, it just initializes some variables and executes automatically a method that makes some processing. 第二个活动没有任何UI,它只是初始化一些变量并自动执行进行一些处理的方法。

First I start this method calling it from onCreate, but inside the method I use some functionalities that must implement some interfaces, so, I think that these methods are being executed before they have had time to initialize. 首先,我从onCreate开始调用此方法,但是在方法内部,我使用了一些必须实现某些接口的功能,因此,我认为这些方法在没有时间初始化之前就已经在执行。

So my question is about how to start automatically a method when I start a new activity, but giving to it some time before starting, to let the implemented interfaces initialize. 所以我的问题是,当我开始一个新的活动时如何自动启动一个方法,但是要在开始前花一些时间让它初始化实现的接口。

UPDATE-- UPDATE--

public class GameActivity extends Activity implements TextToSpeech.OnInitListener {

    private static TextToSpeech tts;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        tts = new TextToSpeech(this, this);
        //...
        SpeechWhenMotion();
    }


    public void SpeechWhenMotion() {
        //...
        tts.speak("Inicializando...", TextToSpeech.QUEUE_ADD, null);
        //...
    }

It doesn't speak because it does not have time to call onInit method from onInitListener. 它不会讲话,因为它没有时间从onInitListener调用onInit方法。

You should move your call to SpeechWhenMotion() from onCreate() to the onInit() method from TextToSpeech.OnInitListener . 您应该SpeechWhenMotion()调用从onCreate()移到TextToSpeech.OnInitListeneronInit()方法。 The whole point of the TextToSpeech.OnInitListener is that you get the callback to onInit() when the initialisation is complete. TextToSpeech.OnInitListener的全部要点是,初始化完成后,您将获得对onInit()的回调。

You are correct that calling it in onCreate() is too early - you must wait for onInit(). 您正确地在onCreate()中调用它为时过早-您必须等待onInit()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM