Equivalent to javax.swing.Timer in Android

前端 未结 3 957
无人共我
无人共我 2021-01-06 01:45

Is there something that looks like the javax.swing.Timer on Android. I know how to create own Threads, but is there something that is like the swing-timer?

3条回答
  •  無奈伤痛
    2021-01-06 02:14

    There is also Java's TimerTask. Here's an example from my code where I'm playing audio samples:

    import java.util.Timer;
    import java.util.TimerTask;
    
    // from constructor, shown here out of place
    timer = new Timer();
    
    // and in method, again, shown out of place:
            INTERVAL_MILLISECONDS = (int)((double)(bufSize) / (double)(nativeSampleRate * 2) * 1000);
            timer.scheduleAtFixedRate( new TimerTask() {
                    public void run() {
                            synchronized(this){
                                    track.write(data, 0, bufSize);
                                    track.play();
                            }
                    }
            }, 0, INTERVAL_MILLISECONDS);
    

提交回复
热议问题