Android的倒计时器

时间:2011-09-13 12:14:26

标签: android countdown

我为倒数计时器制作了单独的项目,它运作正常。现在我必须在我的游戏中应用它。所以我需要一个建议,应该调用计时器?在线程中,在主要课程中,我提供游戏视图参考或游戏视图课程?

2 个答案:

答案 0 :(得分:1)

由于计时器本身是一个线程,每个计时器都有一个线程,在该线程上顺序执行任务。当此线程忙于运行任务时,可运行的任务可能会受到延迟。所以你应该从Main类调用你的Thread

答案 1 :(得分:0)

CountDownTimer将在TextView中显示格式化为天,小时,分钟和秒的时间:

public class DemotimerActivity extends Activity {
        /** Called when the activity is first created. */
        TextView tv;
        long diff;
        long milliseconds;
        long endTime;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            tv = new TextView(this);
            this.setContentView(tv);
            SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
            formatter.setLenient(false);


            String oldTime = "21.10.2013, 12:00";
            Date oldDate;
            try {
                oldDate = formatter.parse(oldTime);
                 milliseconds = oldDate.getTime();

                //long startTime = System.currentTimeMillis();
                // do your work...
                long endTime=System.currentTimeMillis();

                 diff = endTime-milliseconds;       

                Log.e("day", "miliday"+diff);
                long seconds = (long) (diff / 1000) % 60 ;
                Log.e("secnd", "miliday"+seconds);
                long minutes = (long) ((diff / (1000*60)) % 60);
                Log.e("minute", "miliday"+minutes);
                long hours   = (long) ((diff / (1000*60*60)) % 24);
                Log.e("hour", "miliday"+hours);
                long days = (int)((diff / (1000*60*60*24)) % 365);
                Log.e("days", "miliday"+days);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            Long serverUptimeSeconds = (System.currentTimeMillis() - milliseconds) / 1000;


                String serverUptimeText = String.format("%d days %d hours %d minutes %d seconds",
                serverUptimeSeconds / 86400,
                ( serverUptimeSeconds % 86400) / 3600 ,
                ((serverUptimeSeconds % 86400) % 3600 ) / 60,
                ((serverUptimeSeconds % 86400) % 3600 ) % 60
                );


            Log.v("jjj", "miliday"+serverUptimeText);
            MyCount counter = new MyCount(milliseconds,1000);
            counter.start();


        }


        // countdowntimer is an abstract class, so extend it and fill in methods
        public class MyCount extends CountDownTimer {
            public MyCount(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
            }

            @Override
            public void onFinish() {
                tv.setText("done!");
            }

            @Override
            public void onTick(long millisUntilFinished) {
                //tv.setText("Left: " + millisUntilFinished / 1000);

                long diff = endTime - millisUntilFinished; 
                Log.e("left", "miliday"+diff);
                long seconds = (long) (diff / 1000) % 60 ;
                //Log.e("secnd", "miliday"+seconds);
                long minutes = (long) ((diff / (1000*60)) % 60);
                //Log.e("minute", "miliday"+minutes);
                long hours   = (long) ((diff / (1000*60*60)) % 24);
                //Log.e("hour", "miliday"+hours);
                int days = (int)((diff / (1000*60*60*24)) % 365);
                Log.v("days", "miliday"+days);


                Long serverUptimeSeconds = 
                        (System.currentTimeMillis() - millisUntilFinished) / 1000;


                    String serverUptimeText = 
                    String.format("%d days %d hours %d minutes %d seconds",
                    serverUptimeSeconds / 86400,
                    ( serverUptimeSeconds % 86400) / 3600 ,
                    ((serverUptimeSeconds % 86400) % 3600 ) / 60,
                    ((serverUptimeSeconds % 86400) % 3600 ) % 60
                    );  

                    Log.v("new its", "miliday"+serverUptimeText);

                 // tv.setText(days +":"+hours+":"+minutes + ":" + seconds);

                    tv.setText(serverUptimeText);
            }
        }
    }