在我的应用程序中,我使用处理程序来更新日期和时间。它更新日期和时间,但没有给出准确的结果。就像我设置在 60 秒后更新一样,它会在 62 秒后更新。如果我将它更新到 120 秒,它会在 2 分 2 秒后更新。请帮助我为什么说的时间更长?我的代码是
public static String interval = "60";
Handler h2 = new Handler();
Runnable run = new Runnable() {
@Override
public void run() {
MyLocation loc = new MyLocation(getBaseContext());
TextView longitude_view = (TextView) findViewById(R.id.GPS_Long_txta);
((TextView) longitude_view)
.setText(" " + String.valueOf(loc.gps_lon));
TextView latitude_view = (TextView) findViewById(R.id.GPS_Lat_txta);
((TextView) latitude_view).setText(" " + String.valueOf(loc.gps_lat));
long delaytime = (RestoreRecord0()*1000);
last_update_time_txtbx = (TextView) findViewById(R.id.last_update_time_txt);
last_update_time_txtbx.setText(" "
+ String.valueOf(java.text.DateFormat.getDateTimeInstance()
.format(Calendar.getInstance().getTime())));
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String response = CallWebService(tm.getDeviceId(), loc.gps_lon, loc.gps_lat);
h2.postDelayed(this, delaytime);
}
};
这是一个方法RestoreRecord0
public int RestoreRecord0() {
try {
File dir = getFilesDir();
File file = new File(dir, Saveinterval);
if (!file.exists()) {
return Integer.parseInt(interval);
}
FileInputStream fileIS = new FileInputStream(file);
BufferedReader buf = new BufferedReader(new InputStreamReader(
fileIS));
String readString = new String();
String data = "";
// just reading each line and pass it on the debugger
while ((readString = buf.readLine()) != null) {
data += readString;
}
return Integer.parseInt(data);
}
catch (Exception ex) {
ex.getMessage();
}
return Integer.parseInt(interval);
}
请帮忙。如何定期准确地更新时间?