0

我发现一个带有两个按钮的活动可以启动和停止计时器,但如果没有按钮就无法使其工作,我希望它在创建时自行启动。我试图像这样放置一个runnable,但它没有用

class Starter implements Runnable {
            public void run() {
                mCount= 0;
                timerStart();

            }

    }

这是我的主要活动

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

      TextView mTextView;
        Button   mButtonStart;
        Button   mButtonStop;

        int      mCount;
        Timer    mTimer;

        Handler  mHandler= new Handler();

        Runnable Time= new Runnable() {
            @Override 
            public void run() {
                mCount++; 
                mTextView.setText("Count="+mCount);
            }
        };

        protected class TheTimerTask extends TimerTask {
            @Override public void run() {
                // What we want to say is
                //  mCount++; 
                //  mTextView.setText("Count="+mCount);
                // But this gives "Only the original thread that created a view hierarchy can touch its views."
                mHandler.post(Time);
            }
        }






        protected void timerStart() {
            if( mTimer==null ) {
                mTimer = new Timer();
                mTimer.schedule( new TheTimerTask(), 0, 100 ); // 100 milli seconds
            }
        }

        protected void timerStop() {
            if( mTimer!=null ) {
                mTimer.cancel(); 
                mTimer= null;
            }
        }


        OnClickListener mButtonStart_OnClick = new OnClickListener() {
            @Override public void onClick(View v) {
                mCount= 0;
                timerStart();
            }
        };

        OnClickListener mButtonStop_OnClick = new OnClickListener() {
            @Override public void onClick(View v) {
                timerStop();
            }
        };


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

            mTextView= (TextView)findViewById(R.id.textview);
            mButtonStart= (Button)findViewById(R.id.buttonstart);
            mButtonStop= (Button)findViewById(R.id.buttonstop);

            mButtonStart.setOnClickListener(mButtonStart_OnClick);
            mButtonStop.setOnClickListener(mButtonStop_OnClick);

        }
    }

和我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textview"
     android:text="Press start to start timer"/>

<Button android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:id="@+id/buttonstart" 
     android:text="Start"></Button>

<Button android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
     android:text="Stop"
      android:id="@+id/buttonstop"></Button>

</LinearLayout>
4

2 回答 2

1

请不要将 Timers/TimerTasks 用于创建新线程和您面临的问题之类的事情,请改用处理程序

于 2013-10-06T07:43:29.387 回答
0

执行以下操作:

private Handler handler = new Handler();
handler.postDelayed(runnable, 100);

private Runnable runnable = new Runnable() {
@Override
public void run() {
  /* do what you need to do */
  foobar();
  /* and here comes the "trick" */
  handler.postDelayed(this, 100);
}
};

如果您需要更新 UI,则可以使用runOnUiThread. 您可以在此处找到有关为什么对此类任务使用处理程序而不是 timerTask 的详细信息。

于 2013-10-06T08:00:28.037 回答