0

我在 android Java 中获得了 Handler 的代码片段,它告诉我删除 postDelayed 参数周围的括号。

Handler handler = new Handler();
handler.postDelayed (runable, minute);
private Runnable runnable = new Runnable()
{
    @Override
    public void run()
    {
        biomass = biomass +1;
    }
};

我希望程序在一分钟后将生物量增加 1。上面已将分钟声明为 int minute = 60000;

我使用的代码片段在这里:www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/

4

3 回答 3

1

试试这个:

final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                          public void run() {
                              biomass = biomass +1;
                          }
                        }, 6000);
于 2013-04-03T12:56:06.407 回答
0

您可以使用 Thread.sleep(6000) 暂停 Thread/Runnable,其中 6000 是毫秒数

private Runnable runnable = new Runnable()
{
    @Override
    public void run()
    {
        Thread.sleep(6000);
        biomass = biomass +1;
    }
};

新线程(可运行).start();

于 2013-04-03T12:46:51.400 回答
0

您在定义它之前访问 runnable。private 不能在那里定义

    Handler handler = new Handler();
    final Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {

        }
    };
    handler.postDelayed (runnable,c);
于 2013-04-03T12:56:32.120 回答