TimerTask的不执行? [英] TimerTask not executing?

查看:295
本文介绍了TimerTask的不执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的定时器类,这个类的目的是要不断更新视图中的一个计时器。然而,当我运行应用程序时显示的第一个敬酒消息到屏幕,但第二个是永远达不到(永远不会执行一个TimerTask的运行的方法)。我知道,这可能是一些简单的,我做错了。如果任何人都可以引导我在正确的direcion这将是巨大的。

 公共类MyTimer {静态定时_timerTask =新的Timer();
静态INT totalSeconds = 1,小时= 0,最小= 0,仲= 0;
静态字符串mTimeFormat =%02D:%02D:%02D;
静态字符串timeTakenString;公共静态无效的start(){
    Toast.makeText(GPSMain.context,消息一,Toast.LENGTH_LONG).show();    TimerTask的计时器=新的TimerTask(){
        @覆盖
        公共无效的run(){
            Toast.makeText(GPSMain.context,消息二,Toast.LENGTH_LONG).show();
            totalSeconds + = 1;
            秒+ = 1;
            如果(SEC> = 60){
                秒= 0;
                分+ = 1;
                如果(分> = 60){
                    分= 0;
                    小时+ = 1;
                }
            }
            timeTakenString =的String.format(mTimeFormat,时,分,秒);
            postExecute.sendEmptyMessage(0); //更新UI
        }
        私人处理程序postExecute =新的处理程序(){
            公共无效DispatchMessage函数(消息MSG){
                super.dispatchMessage(MSG);
                GPSMain.timer.setText(拍摄时间:+ timeTakenString);
            }
        };
    };
_timerTask.scheduleAtFixedRate(定时器,1000,1000);
}
}

code在另一个文件中调用这个类:

  MyTimer myTimer =新MyTimer();
....
myTimer.start();

项目SPEC变了!
我的项目领导改变项目的规范,以便它不再需要更新计时器到UI而是显示为一个最终结果。反正接受第一个答案,因为它解决了原来的问题。将发布以下新的code。

新的code调用:

  System.currentTimeMillis的();

在runcycle,它返回一个长的一开始就和结束。的第一个值,然后从第二个值中减去以计算执行runcycle所花费的时间量。然后该值被操纵并投入即在末端作为字符串显示的计时器格式

 公共静态字符串getTimeTaken(长端,长开始){
    @燮pressWarnings(未使用)
    串FORMATTEDTIME =,hourHour =,hourMin =:,MINSEC =:;
    长timeTaken =(最终开始)/ 1000,小时= 0,最小为0,秒= 0;
    如果(timeTaken> 9){
        hourHour =0;
        hourMin =0;
        如果(timeTaken> = 60){
            如果(timeTaken> = 3200){
                小时= timeTaken / 3200;
                timeTaken = timeTaken%3200;
                如果(小时> 9){
                    hourHour =;
                }
            }
            分= timeTaken / 60;
            timeTaken = timeTaken%60;
            如果(分> 9){
                hourMin =:;
            }
        }
        秒= timeTaken;
        如果(秒%60℃; 10){
            MINSEC =0;
        }
        返回FORMATTEDTIME =(hourHour +小时+ hourMin +分+ MINSEC +秒);
    }
    秒= timeTaken;
    MINSEC =0;
    hourMin =0;
    hourHour =0;
    返回FORMATTEDTIME =(hourHour +小时+ hourMin +分+ MINSEC +秒);
}


解决方案

使用线程,你不能更新你的用户界面为你必须使用runOnUiThread

  youractivity.this.runOnUiThread(新的Runnable(){公共无效的run(){Toast.makeText(mContext,消息,Toast.LENGTH_LONG).show();}} );

Here is my timer class, This class is designed to constantly update a timer in a view. However, when I run the app the first toast message is displayed to the screen but the second one is never reached (the timerTask's "run" method is never executed). I know that this is probably something simple that I am doing wrong. If anyone could steer me in the right direcion that would be great.

public class MyTimer  { 

static Timer _timerTask = new Timer();
static int totalSeconds = 1, hour = 0, min = 0, sec = 0;
static String mTimeFormat = "%02d:%02d:%02d";
static String timeTakenString;



public static void start (){
    Toast.makeText(GPSMain.context, "Message one", Toast.LENGTH_LONG).show();

    TimerTask timer = new TimerTask() {
        @Override
        public void run() {
            Toast.makeText(GPSMain.context, "Message two", Toast.LENGTH_LONG).show();
            totalSeconds += 1;
            sec += 1;
            if(sec >= 60) {
                sec = 0;
                min += 1;
                if (min >= 60) {
                    min = 0;
                    hour += 1;
                }
            }
            timeTakenString = String.format(mTimeFormat, hour, min, sec);
            postExecute.sendEmptyMessage(0); //update UI
        }
        private Handler postExecute = new Handler(){
            public void dispatchMessage(Message msg) {
                super.dispatchMessage(msg);
                GPSMain.timer.setText("Time Taken: "+timeTakenString);
            }
        };
    };
_timerTask.scheduleAtFixedRate(timer,1000,1000);
}
}

code in another file calling this class:

MyTimer myTimer = new MyTimer();
....
myTimer.start();

PROJECT SPEC CHANGED! My project leader changed the spec of the project so that it no longer needs to update the timer to the UI but rather display it as an end result. Accepting the first answer anyway as it solves the original problem. Will post the new code below.

New code calls:

 System.currentTimeMillis();

at the beggining and end of the runcycle, which returns a long. The first value is then subtracted from the second value to calculate the amount of time taken to execute the runcycle. That value is then manipulated and put into a timer format that is displayed at the end as a string.

public static String getTimeTaken(long end, long start){
    @SuppressWarnings("unused")
    String formattedTime = "", hourHour = "", hourMin = ":", minSec = ":";
    long timeTaken = (end-start)/1000, hour = 0, min = 0, sec = 0;
    if (timeTaken>9 ){
        hourHour = "0";
        hourMin = ":0";
        if (timeTaken>=60){
            if (timeTaken>= 3200){
                hour = timeTaken/3200;
                timeTaken = timeTaken%3200;
                if (hour>9){
                    hourHour = "";
                }
            }
            min = timeTaken/60;
            timeTaken = timeTaken%60;
            if (min >9){
                hourMin = ":";
            }
        }
        sec = timeTaken;
        if(sec%60<10){
            minSec = ":0";
        }
        return formattedTime = (hourHour+hour+hourMin+min+minSec+sec);
    }
    sec = timeTaken;
    minSec = ":0";
    hourMin = ":0";
    hourHour = "0";
    return formattedTime = (hourHour+hour+hourMin+min+minSec+sec);
}

解决方案

Using thread you cant update your UI for that you have to use runOnUiThread

youractivity.this.runOnUiThread(new Runnable(){public void run(){Toast.makeText(mContext, "Message", Toast.LENGTH_LONG).show();}});

这篇关于TimerTask的不执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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