Sam
Sam

Reputation: 13

Countdown timer for android

I have made separate project for countdown timer and it is working fine. Now i have to apply that in my game. So i need a suggestion where the timer should be called? In thread, in main class where i am giving gameview refernce or in a gameview class?

Upvotes: 1

Views: 856

Answers (2)

kyogs
kyogs

Reputation: 6836

CountDownTimer that will display the time formatted to your days, hours, minutes, and seconds all in a 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);
            }
        }
    }

Upvotes: 0

Som
Som

Reputation: 1544

As timer itself is a thread and Each timer has one thread on which tasks are executed sequentially. When this thread is busy running a task, runnable tasks may be subject to delays. So you should call your Thread from the Main class

Upvotes: 1

Related Questions