Android:用于启动和停止循环的按钮

时间:2015-03-15 10:10:06

标签: android runnable timertask

我想在按下按钮时重复更新布局中的某些图像视图,然后在再次按下相同按钮时停止更新。代码完成如下:

代码:

    btn_start.setOnClickListener(new View.OnClickListener()    
    {
        public void onClick(View view) 
        {   
            machinepress++;
            if (machinepress%2 ==1)
            {
                start_machine_keyboard_refresh();   
            }
            else
            {
                stop_machine_keyboard_refresh();
            }               
        }
    }); 


private TimerTask timerTask = new TimerTask() 
    {
        @Override
        public void run() 
        {
            try 
            {
                runOnUiThread(new Runnable() 
                {
                    @Override
                    public void run() 
                    {
                        refresh_machine_keyboard();
                    }
                });
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    };

    public void start_machine_keyboard_refresh() 
    {
        if(timer != null) 
        {
            return;
        }
        timer = new Timer();
        timer.scheduleAtFixedRate(timerTask, 0, 100);
    }

public void stop_machine_keyboard_refresh() {
    timer.cancel();
    timer = null;
}

logcat的:

03-15 17:53:27.316: W/dalvikvm(7051): threadid=1: thread exiting with uncaught exception (group=0x4172fda0)
03-15 17:53:27.316: E/AndroidRuntime(7051): FATAL EXCEPTION: main
03-15 17:53:27.316: E/AndroidRuntime(7051): Process: com.abc.abc, PID: 7051
03-15 17:53:27.316: E/AndroidRuntime(7051): java.lang.IllegalStateException: TimerTask is scheduled already
03-15 17:53:27.316: E/AndroidRuntime(7051):     at java.util.Timer.scheduleImpl(Timer.java:572)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at java.util.Timer.scheduleAtFixedRate(Timer.java:528)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at com.abc.abc.LuckyNumber.start_machine_keyboard_refresh(LuckyNumber.java:840)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at com.abc.abc.LuckyNumber$9.onClick(LuckyNumber.java:352)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at android.view.View.performClick(View.java:4633)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at android.view.View$PerformClick.run(View.java:19331)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at android.os.Handler.handleCallback(Handler.java:733)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at android.os.Looper.loop(Looper.java:157)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at android.app.ActivityThread.main(ActivityThread.java:5377)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at java.lang.reflect.Method.invokeNative(Native Method)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at java.lang.reflect.Method.invoke(Method.java:515)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-15 17:53:27.316: E/AndroidRuntime(7051):     at dalvik.system.NativeStart.main(Native Method)

问题:

如何修改代码以解决java.lang.IllegalStateException: TimerTask is scheduled already问题?谢谢!

1 个答案:

答案 0 :(得分:0)

感谢samgak的链接。我已经更新了它,它现在正在运行:

private class MyTimerTask extends TimerTask
    {
        @Override
        public void run() 
        {
            try 
            {
                runOnUiThread(new Runnable() 
                {
                    @Override
                    public void run() 
                    {
                        refresh_machine_keyboard();
                    }
                });
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    };

    public void start_machine_keyboard_refresh() 
    {
        if(timer != null) 
        {
            return;
        }
        timer = new Timer();
        TTimerTask = new MyTimerTask();
        timer.scheduleAtFixedRate(TTimerTask, 0, 100);
    }

    public void stop_machine_keyboard_refresh() {
        timer.cancel();
        timer = null;
    }