如何检查一番定期? [英] How to check something at regular intervals?
问题描述
新手在这里,我有一个简单的问题。
Beginner here, I have a simple question.
在Android的什么是最好的东西来定期检查些什么呢?
In Android what would be the best what to check for something at regular intervals?
请多多包涵,我会尽力解释尽我所能 -
Please bear with me, I'll try to explain the best I can --
例如我的音乐应用程序是非常简单的,主要活动和服务。主要活动有两个按钮,启动和停止音频的UI。我preSS启动和音频服务启动。同样,当我点击停止服务停止,音频结束。如果 isLooping()
是硬coded到真正有没有问题,因为声音永远不会结束,除非我打了停止按钮,用于停止音频服务,并重置按钮状态。
For example my audio app is very simple, a main activity and a service. The main activity has a UI with two buttons, start and stop audio. I press start and the audio service starts. Likewise when I click Stop the service stops and the audio ends. If isLooping()
is hard-coded to true there is no issue because the audio never ends unless I hit stop button, which stops the audio service and also resets the button states.
这是一个问题,因为我设置 isLooping()
为false,这样的声音不循环。因此,音频将停止播放,但服务仍然在运行。
This is an issue now because I set isLooping()
to false so the audio doesn't loop. So the audio will stop playing but the service is still running.
我希望能够检测到时,音频停止,所以我可以设置的UI按钮的状态。所以,我需要的东西,总是检查是否音频播放(即检查 player.isPlaying()
这样我就可以结束服务,设置启动/关闭按钮的状态。
I want to be able to detect when the audio stops so I can set the states of the UI buttons. So I need something that is always checking whether audio is playing (i.e. check player.isPlaying()
so I can end the service and set the enable/disable state of the buttons.
我想通了绑定到该服务,所以我可以通过我的主要活动访问的MediaPlayer
控制,所以我知道的code,以检查它是否发挥,但如果我把这个code所以它检查了所有的时间呢?
I figured out binding to the service so I can access the MediaPlayer
controls via my main activity so I know the code to check if it's playing, but WHERE do I put this code so it's checked all the time?
我是不是做有意义吗?我知道这可能是很简单的。感谢您的帮助。
Am I making sense? I know this is probably very simple. Thanks for any help.
推荐答案
您可以使用一个TimerTask和Timer重复。低于code:
You can repeat it with the TimerTask and Timer. Code below:
public final void RepeatSoundFunction(){
t = new Timer();
tt = new TimerTask() {
@Override
public void run() {
mp.seekTo(0); //Reset sound to beginning position
mp.start(); //Start the sound
t.purge(); //Purge the sound
}
};
t.schedule(tt, 10*1000); //Schedule to run tt (TimerTask) again after 10 seconds
}
然后设置一个MediaPlayer的onCompletionListener,并在那里你把这个。
then you set a MediaPlayer onCompletionListener and in there you put this.
在运行 - code,你可以检查比其他的东西 音乐,我只显示与音频的例子。
Inside the run-code you can check for other things than music, I just show an example with the audio.
这篇关于如何检查一番定期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!