In my application, I'm using a handler to update date and time. It updates date & time but not given the accurate result. Like if I set to update after 60 seconds, it update after 62 seconds. If I update it to 120 sec, it update after 2 min 2 sec. Kindly help me why it is talking more time? My code is
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);
}
};
Here is a method 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);
}
Kindly help. How to update of time at regular and accurate interval?