Setting A Timer For A Timeout - New Timer() Fails After Cancel And Purge
I have some location finding code and while I'm waiting for a read of an appropriate accuracy I'd also like to have a timeout and use whatever the best location read is up to that
Solution 1:
Your code is very similar to code I have - that is working. But some differences I notice are that
- you may need to create the new timer as a named daemon - for example
locationTimer = new Timer("locationtimer", true);
- you may need to create a new TimerTask each time you schedule (I assume timeoutReached is the TimerTask?) - for example
locationTimer.schedule(new timeoutReached(), 60000);
Solution 2:
Timer doesn't work correctly on UI. Never. Use Handler. You can take also my class for it: It start/ stops the timing and shows the time and/or date in the registered TextView fields.
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.os.Handler;
import android.widget.TextView;
/**
* The class for creating and refreshing many different fields on different layouts,
* that can hold actual time and/or date in different formats
* The formats should be as in http://developer.android.com/reference/java/text/SimpleDateFormat.html.
* Only present and visible fields are being actualized, so there is no need to clean the clock list after closing an activity
*
* Examples of use:
*
* Clock.registerClock((TextView) findViewById(R.id.TimeField), "HH:mm");
* Clock.registerClock((TextView) findViewById(R.id.DateField), "d.M.yyyy EEE");
* Clock.start(10000L);
*
* @author Petr Gangnus
*/publicfinalclassClock {
/**
* the handler that works instead of timer and supports UI
*/staticprivateHandlerhandler=newHandler();
/**
* the interval of the time refreshing
*/staticprivatelong refreshStep;
/**
* pairs TextView timer+time/date format
*/private TextView clockFace;
private String format;
privateClock(TextView clockFace, String format){
this.clockFace=clockFace;
this.format=format;
}
// here is the list of views containing the visual timers that should be held actualstaticprivate ArrayList<Clock> clocks=newArrayList<Clock>();
/**
* fills all timer fields by actual time value, according to their formats.
*/staticprivateRunnablemUpdateTimeTask=newRunnable() {
publicvoidrun() {
for(Clock clock:clocks){
showActualTimeDate(clock);
}
handler.postDelayed(this,refreshStep);
}
};
//============================================ public members ====================================================================/**
* add a clock to the list of updating clocks
* @param clockFace - the place where the time or date will be shown
* @param format - the format of the time/date
* @return
*/publicstaticbooleanregisterClock(TextView clockFace, String format){
if (clockFace==null) returnfalse;
if(clocks.contains(clockFace)){
// old clockFace
clocks.get(clocks.indexOf(clockFace)).format=format;
} else {
// new clockFace
clocks.add(newClock(clockFace, format));
}
returntrue;
}
/**
* remove a clock from the updating list
* @param clockFace
* @return
*/publicstaticbooleanunRegisterClock(TextView clockFace){
if (clockFace==null) returnfalse;
if(clocks.contains(clockFace)){
// found clockFace
clocks.remove(clocks.indexOf(clockFace));
} else {
// not found clockFacereturnfalse;
}
returntrue;
}
/**
* put in the "place" the actual date/time in the appropriate "format"
* @param place
* @param format
*/publicstaticvoidshowActualTimeDate(Clock clock){
if (clock.clockFace==null) return;
if (clock.clockFace.getVisibility()!=TextView.VISIBLE) return;
Date thisDate=newDate();
SimpleDateFormat df=newSimpleDateFormat(clock.format);
clock.clockFace.setText(df.format(thisDate));
}
/**
* start the ticking for all clocks
* @param step the tick interval
*/publicstaticvoidstart(long step) {
refreshStep=step;
handler.removeCallbacks(mUpdateTimeTask);
handler.postDelayed(mUpdateTimeTask, 0);
}
/**
* Stopping ticking all clocks (not removing them)
* the calling could be put somewhere in onStop
*/publicstaticvoidstop() {
handler.removeCallbacks(mUpdateTimeTask);
}
}
Post a Comment for "Setting A Timer For A Timeout - New Timer() Fails After Cancel And Purge"