我的应用会检查是否有带日期的文件。
如果有一个日期,它会计算今天和那个(未来)日期之间的差异,并初始化一个计时器,按照X天Y小时Z分钟S秒的方式计算该日期之前的秒数。
如果没有文件,则用户可以使用按钮选择日期。程序会将日期存储在文件中并设置倒计时。
有一个删除按钮可删除日期并选择另一个。此删除按钮应取消计时器,以便停止计数。
取消时,计时器不会停止。我的计时器正在滴答作响,但差异是2秒而不是1秒。它显示40 38 36 ...而不是40 39 38 ...
最后,它将选择的日期存储两次而不是一次。
DatePicker代码来自here。
public class NextVisit extends Activity implements DatePickerFragment.TheListener{
protected Vibrator vibrate;
protected int SECONDS_IN_A_DAY = 24 * 60 * 60;
protected String filePath = "";
protected SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
final long[] pattern = {0, 1000, 500, 1000, 500, 1000, 500, 1000 };
CountDownTimer timer = null;
String dateString = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next_visit);
final TextView label = (TextView) findViewById(R.id.label);
final TextView countdown = (TextView) findViewById(R.id.time_visit);
final Button bDate = (Button) findViewById(R.id.date_button);
final Button bDelete = (Button) findViewById(R.id.delete_button);
File dir = new File(getFilesDir(), "loveApp");
if(!dir.exists())
dir.mkdirs();
final File f = new File(getFilesDir()+"/loveApp"+"/love_date.txt");
filePath = f.getAbsolutePath();
vibrate = (Vibrator) getSystemService(VIBRATOR_SERVICE);
bDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(f.exists()){
f.delete();
System.out.println("File deleted successfully");
bDate.setVisibility(View.VISIBLE);
label.setText(R.string.next_visit1);
timer.cancel();
countdown.setText("");
System.out.println("I cancelled the timer");
bDelete.setVisibility(View.INVISIBLE);
bDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
bDelete.setVisibility(View.VISIBLE);
bDate.setVisibility(View.INVISIBLE);
picker.show(getFragmentManager(), "datePicker");
}
});
}
}
});
if(f.exists()){
System.out.println("File exists");
bDate.setVisibility(View.INVISIBLE);
label.setText(getString(R.string.label));
try{
BufferedReader br = new BufferedReader(new FileReader(f));
dateString = br.readLine();
br.close();
System.out.println("Date: "+dateString);
Date finalDate = formatter.parse(dateString);
setCountdown(finalDate);
}catch(Exception e){
e.printStackTrace();
label.setText("ERROR");
}
}else{
System.out.println("File DOES NOT EXIST");
bDelete.setVisibility(View.INVISIBLE); //Removing the delete date button
bDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
bDelete.setVisibility(View.VISIBLE);
bDate.setVisibility(View.INVISIBLE);
picker.show(getFragmentManager(), "datePicker");
}
});
}
}
public void setCountdown(Date finishDate){
long end = finishDate.getTime();
final TextView countdown = (TextView) findViewById(R.id.time_visit);
timer = new CountDownTimer(end, 1000){
@Override
public void onTick(long millisUntilFinished) {
long now = Calendar.getInstance(Locale.GERMANY).getTimeInMillis()+1000;
long diff = millisUntilFinished - now;
long diffSec = diff / 1000;
long days = diffSec / SECONDS_IN_A_DAY;
long secondsDay = diffSec % SECONDS_IN_A_DAY;
long seconds = secondsDay % 60;
long minutes = (secondsDay / 60) % 60;
long hours = (secondsDay / 3600);
countdown.setText(days+ " days, "+ hours + "hours, "+minutes + "m, "+seconds + "s remaining!");
}
@Override
public void onFinish() {
countdown.setText("done!");
vibrate.vibrate(pattern,-1);
}
}.start();
}
@Override
public void returnDate(String date) {
dateString = date;
TextView label = (TextView) findViewById(R.id.label);
label.setText(R.string.label);
BufferedWriter bw;
try {
bw = new BufferedWriter(new FileWriter(new File(filePath)));
bw.write(dateString);
bw.close();
System.out.println("Saved the file, date is: "+ dateString);
Date finishDate = formatter.parse(date);
setCountdown(finishDate);
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
我认为你需要把这个字符串long now = Calendar.getInstance(Locale.GERMANY).getTimeInMillis()+1000;
中的问题放在倒计时器代码之外。
final long now = Calendar.getInstance(Locale.GERMANY).getTimeInMillis()+1000;
timer = new CountDownTimer(end, 1000){
@Override
public void onTick(long millisUntilFinished) {
long diff = millisUntilFinished - now;
long diffSec = diff / 1000;
...
};