在Android应用程序屏幕计时器? [英] On screen timer in Android application?

查看:111
本文介绍了在Android应用程序屏幕计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和朋友谁试图创建一个游戏要使用计时器作为得分的球员的一种方式,所以我们要显示屏幕上的计时器从0开始在比赛的开始和停止时,他们完成比赛。

My friends and I who are trying to create a game want to use a timer as a way of scoring players, and so we want to display an on-screen timer starting at 0 at the start of the game and stopping when they finish the game.

我已经看过一些其他职位,以及Android开发人员网站上为通过处理器,为了使其工作,因为我想一个Runnable对象,但现在的运行(该Runnable对象正在内)方法完全跳过了,所以什么都没有做。现在,我有我们的活动的onCreate方法内的以下内容:

I have looked at some other posts as well as on the android dev website into using a Handler and a Runnable object in order to make it work as I want, but as of right now the run() method inside of the Runnable object is being completely skipped over, so nothing is being done. Right now, I have the following inside of our Activity's onCreate method:

    mHandler = new Handler();
    mStartTime = System.currentTimeMillis();
    mHandler.removeCallbacks(mUpdateTimeTask);
    mHandler.postDelayed(mUpdateTimeTask, 100);

    mUpdateTimeTask = new Runnable()
    {
        public void run() 
        {
            final long start = mStartTime;
            long millis = SystemClock.uptimeMillis() - start;
            int seconds = (int) (millis / 1000);
            seconds = seconds % 60;

            mHandler.postAtTime(this,
                    (start + seconds + 1) * 1000);

            elapsed.setText(seconds);
        }
     };

现在第一关,谁能告诉我,为什么的run()方法被完全跳​​过?而且,将这项工作我们正在尝试做的?或者我们应该有Runnable对象别的地方?使用不同的方法?这是我们第一次用一个定时器工作,所以任何帮助AP preciated!

Now first off, can anyone tell me why the run() method is being completely skipped? And also, will this work for what we are trying to do? Or should we have the Runnable object somewhere else? Use a different approach? This is our first time working with a timer, so any help is appreciated!

推荐答案

也许你需要调用 mHandler.postDelayed(mUpdateTimeTask,100) mUpdateTimeTask =新的Runnable()分配

Perhaps you need to call mHandler.postDelayed(mUpdateTimeTask, 100) after the mUpdateTimeTask = new Runnable() assignment

这篇关于在Android应用程序屏幕计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆