2013-03-10 37 views
0

我有这个代码,它只是显示一个事件的倒计时钟。与正常时钟不同,我对于最后秒位跳数有问题。这是我正在使用的代码。有人可以提供一些指导,以解决这个问题。亲切的问候。TextView定时器秒数位跳

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); 

} 

}

回答

1

我准备了完整的解决方案,让我知道,如果你有一些问题:

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); 
     } 

    } 
} 
+0

非常感谢你,这是伟大的工作。计算只有一个问题。我定义的日期是2013年4月25日(我在配置文件中将该月定义为3),并且显示器当前仅显示15d 1h 48m 14s,应该在45d 12h左右。我试图解决它,但这是我需要学习的另一件事。 – 2013-03-10 19:22:06

+0

如何将unix时间戳(从1970-01-01开始的毫秒)保存到文件中?恕我直言montsh从零开始计算。 – gingo 2013-03-10 23:34:32

+0

我有正确的日子,小时和分钟显示。我添加了CEST时区(仅用于测试),还需要使用cal.add()减去1个月和1天。 – 2013-03-14 13:22:05