Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我怎样才能在计时器中使我的程序每秒都会做一些事情呢?_Java_Android - Fatal编程技术网

Java 我怎样才能在计时器中使我的程序每秒都会做一些事情呢?

Java 我怎样才能在计时器中使我的程序每秒都会做一些事情呢?,java,android,Java,Android,在MainActivity.java的顶部,我添加了: private Handler customHandler = new Handler(); long timeSwapBuff = 0L; 然后在onCreate中,我做了: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConten

在MainActivity.java的顶部,我添加了:

private Handler customHandler = new Handler();
long timeSwapBuff = 0L;
然后在onCreate中,我做了:

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread,0);

        timerValue = (TextView) findViewById(R.id.timerValue);

        startButton = (Button) findViewById(R.id.startButton);

        startButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(updateTimerThread, 0);

            }
        });

        pauseButton = (Button) findViewById(R.id.pauseButton);
        pauseButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                customHandler.removeCallbacks(updateTimerThread);

            }
        });
然后在updateTimerThread中:

private Runnable updateTimerThread = new Runnable() {

        public void run() {

            long updatedTime = 0L;
            long timeInMilliseconds = 0L;
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;//System.currentTimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            timerValue.setText("" + mins + ":"
                    + String.format("%02d", secs) + ":"
                    + String.format("%03d", milliseconds));
            customHandler.postDelayed(this, 0);
        }

    };
现在,每次我从开始运行程序时,计时器将从00:00:00开始

现在我想加上一些东西,让每一秒都能做点什么。 不重置计时器,不暂停或停止计时器,让它继续运行,但每秒钟我都想在程序中执行其他操作

问题是我如何每秒钟检查一次来做某事