使Java中的android一个间隔定时器 [英] Making a interval timer in Java android
问题描述
我有计划创建使用计时器的时间间隔的应用程序。它应该只是最基本所以我不得不添加更多的时候我已经理解的基础知识。我想实现的是选择分钟的间隔应持续,但有多少次这样的间隔应该去数。这样的最后1分钟,去8次间隔。 现在的问题是定时器最好用?我已经试过了我对Android倒数计时器,它似乎工作。但有另外一个是更好吗?
I have plans to create an interval app using timers. It should just be the most basic So I'll have to add some more when I've understood the basics. What I want to achieve is to select the number of minutes an interval should last, yet how many times this interval should go. Like a interval that last 1 minute and goes 8 times. The question is which timer is best to use? I have tried me on the Android Countdown Timer and it seems to work. But is there another one which is better?
推荐答案
我总是推荐使用处理程序
。
它比内置的类,但我觉得这是大大更有效率,你有更多的控制权多一点的工作。
Its a little more work than the built in classes, but I find that it is vastly more efficient and you have more control over it.
该处理器是一类将处理code运行在一个特定尺蠖
/ 发
默认,它是建立在线程,否则,您可以指定处理程序通过传递尺蠖
到处理程序执行其code code> constuctor像 -
新的处理程序(Looper.getMainLooper());
The Handler is a class that will handle code execution over a specific Looper
/ Thread
by default, the Thread it is created in, Otherwise you can specify where the Handler executes its code by passing in the Looper
to the Handler
constuctor like - new Handler(Looper.getMainLooper());
我之所以会建议尺蠖是因为你的控制提出了更高的灵活性在它作为一个稍微低了下去抽象在的TimerTask
的方法。
The reason I would recommend the looper is because you have a higher flexibility of control over it as its a slightly lower down abstraction over the TimerTask
methods.
和他们一般为每CE执行code跨线程一般,在线程之间的管道数据的方法非常有用的。
And generally they are very useful for executing code across threads generally, a method of piping data across threads per ce.
两种主要的方法有:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final Handler h = new Handler();
h.postDelayed(new Runnable()
{
private long time = 0;
@Override
public void run()
{
// do stuff then
// can call h again after work!
time += 1000;
Log.d("TimerExample", "Going for... " + time);
h.postDelayed(this, 1000);
}
}, 1000); // 1 second delay (takes millis)
}
简单的使用!或者你可以使用的信息,从而降低对象creationg如果你正在考虑高速更新的用户界面等等 - 会降低GC
Simple use! Or you can use messages, which reduce object creationg if you are thinking about high speed updating UI etc - will reduce GC.
类MainActivity延伸活动{
class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
MyTimers timer = new MyTimers();
timer.sendEmptyMessage(MyTimers.TIMER_1);
timer.sendEmptyMessage(MyTimers.TIMER_2);
}
public static class MyTimers extends Handler
{
public static final int TIMER_1 = 0;
public static final int TIMER_2 = 1;
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case TIMER_1:
// Do something etc.
Log.d("TimerExample", "Timer 1");
sendEmptyMessageDelayed(TIMER_1, 1000);
break;
case TIMER_2:
// Do another time update etc..
Log.d("TimerExample", "Timer 2");
sendEmptyMessageDelayed(TIMER_2, 1000);
break;
default:
removeMessages(TIMER_1);
removeMessages(TIMER_2);
break;
}
}
}
}
观测,这是不可以一个完整的实现,但给你做的好方法。
Obs this is not a full implementation but to give you a good way of doing it.
这篇关于使Java中的android一个间隔定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!