java - TextView定时器秒数字跳跃

标签 java android eclipse

我有这段代码,它只是显示事件的倒计时时钟。我对它的最后几秒位置跳跃数字有疑问,这与普通时钟不同。这是我正在使用的代码。有人可以提供一些指导来说明这个问题可能出在哪里吗?亲切的问候。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Timer().schedule(new TimerTask() {

        File sdcard = Environment.getExternalStorageDirectory();
        File cfgFile = new File(sdcard, "TimeTillParis/config.txt");

        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {

                try {
                    InputStream is = new FileInputStream(cfgFile);
                    Properties prop = new Properties();
                    prop.load(is);

                    int mmY = Integer.parseInt(prop.getProperty("YEAR"));
                    int mmM = Integer.parseInt(prop.getProperty("MONTH"));
                    int mmD = Integer.parseInt(prop.getProperty("DAY"));
                    int mmH = Integer.parseInt(prop.getProperty("HOUR"));
                    int mmC = Integer.parseInt(prop.getProperty("MINUTE"));
                    int mmS = Integer.parseInt(prop.getProperty("SECOND"));

                    Calendar cal = Calendar.getInstance();
                    Date today = new Date(System.currentTimeMillis());

                    cal.set(Calendar.YEAR, mmY);
                    cal.set(Calendar.MONTH, mmM);
                    cal.set(Calendar.DAY_OF_MONTH, mmD);
                    cal.set(Calendar.HOUR_OF_DAY, mmH);
                    cal.set(Calendar.MINUTE, mmC);
                    cal.set(Calendar.SECOND, mmS);

                    long milliseconds = (cal.getTimeInMillis() - today.getTime());

                    String mD="", mH="", mM="", mS="", boxText="";

                    mD += (milliseconds / (1000*60*60*24));
                    mH += (milliseconds / (1000*60*60) % 24);
                    mM += (milliseconds / (1000*60) % 60);
                    mS += (milliseconds / 1000 % 60);

                    boxText += "Next visit to Paris in \n\n" +
                                mD + "d   " + 
                                mH + "h   " + 
                                mM + "m   " + 
                                mS + "s";

                    TextView textView = (TextView) findViewById(R.id.textView_date_display);
                    textView.setText(boxText);

                } catch (FileNotFoundException e) {
                    String cfgNotFound="";
                    cfgNotFound += "config.txt not found!";
                    TextView textView = (TextView) findViewById(R.id.textView_date_display);
                    textView.setText(cfgNotFound);
                    //e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                }
            });
        }
    }, 0, 100);

}

}

最佳答案

我准备了完整的解决方案,如果您有任何疑问,请告诉我:

public class MainActivity extends Activity {

    private static final int COUNTDOWN_INTERVAL = 1000;

    MyTimer timer;

    Calendar eventTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        File sdcard = Environment.getExternalStorageDirectory();
        File cfgFile = new File(sdcard, "TimeTillParis/config.txt");

        try {
            InputStream is = new FileInputStream(cfgFile);
            Properties prop = new Properties();
            prop.load(is);

            int mmY = Integer.parseInt(prop.getProperty("YEAR"));
            int mmM = Integer.parseInt(prop.getProperty("MONTH"));
            int mmD = Integer.parseInt(prop.getProperty("DAY"));
            int mmH = Integer.parseInt(prop.getProperty("HOUR"));
            int mmC = Integer.parseInt(prop.getProperty("MINUTE"));
            int mmS = Integer.parseInt(prop.getProperty("SECOND"));

            Calendar cal = Calendar.getInstance();

            cal.set(Calendar.YEAR, mmY);
            cal.set(Calendar.MONTH, mmM);
            cal.set(Calendar.DAY_OF_MONTH, mmD);
            cal.set(Calendar.HOUR_OF_DAY, mmH);
            cal.set(Calendar.MINUTE, mmC);
            cal.set(Calendar.SECOND, mmS);

            eventTime = cal;

        } catch (FileNotFoundException e) {
            String cfgNotFound="";
            cfgNotFound += "config.txt not found!";
            TextView textView = (TextView) findViewById(R.id.textView_date_display);
            textView.setText(cfgNotFound);
            //e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(eventTime != null) {
            long millisInFuture = (eventTime.getTimeInMillis() - System.currentTimeMillis());
            timer = new MyTimer(this, millisInFuture, COUNTDOWN_INTERVAL);
            timer.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(timer != null) {
            timer.cancel();
        }
    }

    private static class MyTimer extends CountDownTimer {

        TextView textView;

        public MyTimer(Activity actitivy, long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            // findViewById is very expansive, so fined it only once
            textView = (TextView) actitivy.findViewById(R.id.textView_date_display);
        }

        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            SimpleDateFormat sdf = new SimpleDateFormat("'Next visit to Paris in \n\n'd'd   'h'h   'm'm   's's'");
            String boxText = sdf.format(new Date(millisUntilFinished));

            textView.setText(boxText);
        }

    }
}

关于java - TextView定时器秒数字跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15322909/

相关文章:

java - 我在制作 Google map 应用程序时收到 : Error:Execution failed for task ':app:transformClassesWithDexForDebug' .

java - 从 Eclipse 的 CMD 访问 sqlite 表

Eclipse-CDT:在自动生成的包含保护中使用命名空间

java - 如何实现具有泛型返回类型和泛型参数的泛型方法?

java - 计算 getter 和 setter

java - 在 json 数组中添加 json 对象,并在 sqlite 的 json 对象中添加 agian json 数组

eclipse - 将现有 Maven 项目自动导入 Eclipse

java - 当文件中只有 1 个字节时,FileInputStream available 方法返回 2

java - VisualVM 在启动时挂起 "computing description"

android - 使用 OpenCV 在人像模式下检测人脸