【问题标题】:Make another class to count time再上一节课来计算时间
【发布时间】:2015-09-17 08:48:15
【问题描述】:

我在创建一个新类来计算时间时遇到问题。这是我的代码:

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

btcheck.setOnClickListener(new View.OnClickListener() {

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

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);     
    };
}

但是它并没有像我预期的那样工作。希望你能帮助我。

【问题讨论】:

  • 也许你会在这里找到答案:stackoverflow.com/questions/4597690/android-timer-how
  • 它没有在 txttime 中显示时间。
  • 伙计,在你的最后一个问题中我提到过,如果一个类只是一个普通类,那么不要用 Activity 扩展它,因为创建一个扩展 Activity 的类的实例是一个非常繁重的问,而不是用一个活动来扩展它,如果你必须使用你的活动类实例,那么制作你的活动类的静态实例并使用它,就像我在你之前的问题中提到的那样。
  • 非常感谢。我在stackoverflow.com/questions/32676002/… 有另一个问题。我希望你能再次帮助我。

标签: android class time


【解决方案1】:

您可以使用扩展 AsyncTask 而不是 MainActivity。这样您就不会创建新的活动积压日志,并且您可以更新您的 UI 而不会与您的线程冲突。

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

【讨论】:

  • 它在“mHandler.postDelayed(mTimeTask,timer);”中显示错误那就是“局部变量 mTimeTask 可能没有被初始化”
  • 嗨,对不起,我忘了关闭方法 startTime()。我已对此答案进行了更改
  • 很抱歉,它不起作用。 1)它没有显示在“txttime”中运行的时间,2)它需要在“new Time(startTime, txttime).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);”中添加@SuppressLint("NewApi")。
  • 取出 .executeOnExecutor(AsyncTask.Serial_EXECUTOR);并替换为通常的 .execute( );
  • 后添加“mTimeTask.run();”它显示应用程序已停止。
猜你喜欢
  • 2017-03-19
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多