Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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 ] + " " );
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...