Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

android - Make another class to count time

I have a problem with create a new class to count time. This is my code:

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

btcheck.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v){
        Time aa = new Time();
        aa.RunTime();                   
    }           
});

And

public class Time extends MainActivity {

    public void RunTime() {     
        startTime = SystemClock.uptimeMillis();
        customHandler.postDelayed(updateTimerThread, 0);            
    }

    public Runnable updateTimerThread = new Runnable() {
        public void run() {

            updatedTime = SystemClock.uptimeMillis() - startTime;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);      

            stime="" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds);                            

            txttime.setText(stime + " ");
            customHandler.postDelayed(this, 0);     
    };
}

However it does'nt work as I expect. Hope you help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use extends AsyncTask instead of MainActivity. This way you're not creating a new activity back log and you can update your UI without conflicting with your threading.

private Handler handler = new Handler( );
    public long startTime;
    public void onClick(View v){
        switch( v.getId( ) ){
            case R.id.btcheck:
                this.startTime = SystemClock.uptimeMillis();
                startTimer( );
                break;
        }
    }
    private void startTimer( ){
        startRunnable( );
    }

    private Runnable startRunnable( )
    {
        new Runnable( )
        {
            @Override
            public void run( )
            {
                new Time( startTime ).execute( );
                long timer = 1000;
                handler.postDelayed( startRunnable( ), timer );
            }
        };
        return null;
    }

    class Time extends AsyncTask< String, String, String >
    {
        private long startTime;
        private long updatedTime;

        public Time( long startTime )
        {
            this.startTime = startTime;
        }

        @Override
        protected void onPreExecute( )
        {
        }

        @Override
        protected void onPostExecute( String result )
        {
        }

        @Override
        protected String doInBackground( String... param )
        {
            updatedTime = SystemClock.uptimeMillis( ) - startTime;

            int secs = ( int ) ( updatedTime / 1000 );
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = ( int ) ( updatedTime % 1000 );

            publishProgress( "" + mins + ":" + String.format( "%02d", secs ) + ":" + String.format( "%03d", milliseconds ) );
            return null;
        }

        @Override
        protected void onProgressUpdate( String... values )
        {
            txttime.setText( values[ 0 ] + " " );
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...