use of java.util.Timer in project glitch-hq-android by tinyspeck.
the class UnlearnFragment method InitUpdateSkillRemainingTimer.
private void InitUpdateSkillRemainingTimer() {
if (m_RemainingTimer != null)
m_RemainingTimer.cancel();
m_RemainingTimer = new Timer();
m_RemainingTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
if (m_unlearningList.size() > 0)
setUnlearningSkill();
else if (m_learningList.size() > 0)
setLearningSkill();
}
});
}
}, 1000, 1000);
}
use of java.util.Timer in project glimmr by brk3.
the class PhotoViewerActivity method startSlideshow.
private void startSlideshow() {
final Handler handler = new Handler();
SharedPreferences defaultSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
final int delay_m = Integer.parseInt(defaultSharedPrefs.getString(Constants.KEY_SLIDESHOW_INTERVAL, "3")) * 1000;
if (BuildConfig.DEBUG)
Log.d(TAG, "slideshow delay: " + delay_m);
mTimer = new Timer();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
int currentPosition = mPager.getCurrentItem();
currentPosition++;
if (currentPosition >= mAdapter.getCount()) {
currentPosition = 0;
}
mPager.setCurrentItem(currentPosition);
}
});
}
}, delay_m, delay_m);
BusProvider.getInstance().post(new PhotoViewerVisibilityChangeEvent(!mActionBar.isShowing(), this));
}
use of java.util.Timer in project Samples-for-Java by blackberry.
the class SimpleLocationProvider method locationUpdated.
/********************* END OF PRIVATE METHODS **********************/
/******************************** LocationListener IMPLEMENTATION ***************************************/
/**
* This is used by this API internally and must NOT be called.
*/
public void locationUpdated(LocationProvider provider, Location location) {
if (provider != this.locationProviderReference) {
// if this is an update from an old location provider that is in the process of being stopped, ignore the update.
return;
}
BlackBerryLocation bbLocation = (BlackBerryLocation) location;
if (bbLocation != null && bbLocation.isValid() && (bbLocation.getQualifiedCoordinates().getLatitude() != 0 && bbLocation.getQualifiedCoordinates().getLongitude() != 0)) {
log("Tracking: Acquired valid location - " + location.getQualifiedCoordinates().getLatitude() + ", " + location.getQualifiedCoordinates().getLongitude());
this.location = (BlackBerryLocation) location;
/**
* retryAttempt should not be reset in MODE_OPTIMAL when currentOptimalModeIsGeolocation
* because last GPS fix might have failed and this is only a fall back to Geolocation that has passed.
* We still need to preserve the retryAttempt until a GPS fix works in MODE_OPTIMAL. Otherwise
* we will simply waste battery by trying GPS fixes too frequently when it is not available.
*/
if (mode != MODE_OPTIMAL || (mode == MODE_OPTIMAL && !currentOptimalModeIsGeolocation)) {
retryAttempt = 0;
log("Tracking: retryAttempt reset to 0");
}
/** Update lastValidFixTime */
lastValidFixTime = System.currentTimeMillis();
log("Tracking: lastValidFixTime updated.");
/** Trigger events via SimpleLocationListener */
if (bbLocation.getGPSMode() == GPSInfo.GPS_MODE_AUTONOMOUS) {
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_GPS_LOCATION, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_GPS_LOCATION");
} else //#ifdef BlackBerrySDK5.0.0
if (bbLocation.getGPSMode() == GPSInfo.GPS_MODE_CELLSITE) {
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_CELL_GEOLOCATION, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_CELL_GEOLOCATION");
} else //#ifdef BlackBerrySDK6.0.0
if (bbLocation.getGPSMode() == LocationInfo.GEOLOCATION_MODE_CELL) {
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_CELL_GEOLOCATION, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_CELL_GEOLOCATION");
} else if (bbLocation.getGPSMode() == LocationInfo.GEOLOCATION_MODE_WLAN) {
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_WLAN_GEOLOCATION, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_WLAN_GEOLOCATION");
} else //#endif
{
// This should not occur
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_UNKNOWN_MODE, bbLocation);
log("Tracking: Location delivered to SimpleLocationListener as EVENT_UNKNOWN_MODE");
}
/** Create a TimerTask to switch to MODE_GPS when appropriate */
if (mode == MODE_OPTIMAL && currentOptimalModeIsGeolocation && isModeAvailable(MODE_GPS)) {
if (restartTimer == null) {
log("Tracking: Scheduling switch to GPS");
switchToGPS = true;
restartTimer = new Timer();
restartTask = new RestartTask();
long nextRetry = getNextRetryDelay();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: GPS will be attempted after " + (nextRetry / 1000) + " seconds.");
}
}
} else {
log("Tracking: Invalid fix.");
/** If Geolocation fails in MODE_OPTIMAL, try GPS (if available) after waiting an appropriate interval */
if (mode == MODE_OPTIMAL && currentOptimalModeIsGeolocation && (((System.currentTimeMillis() - lastValidFixTime) / 1000) > geolocationTimeout)) {
switchToGPS = false;
retryAttempt++;
stopTracking();
if (!isModeAvailable(MODE_GPS)) {
currentOptimalModeIsGeolocation = true;
log("Cannot switch to GPS because MODE_GPS is not available.");
} else {
currentOptimalModeIsGeolocation = false;
log("Switching to GPS");
}
long nextRetry = getNextRetryDelay();
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_LOCATION_FAILED, new Long(nextRetry));
if (restartTimer != null) {
restartTimer.cancel();
restartTimer = null;
}
restartTimer = new Timer();
restartTask = new RestartTask();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: Next attempt after " + (nextRetry / 1000) + " seconds.");
} else /** If GPS fails in MODE_OPTIMAL, try Geolocation (if available) immediately */
if (mode == MODE_OPTIMAL && !currentOptimalModeIsGeolocation && (((System.currentTimeMillis() - lastValidFixTime) / 1000) > gpsTimeout)) {
switchToGPS = false;
retryAttempt++;
stopTracking();
long nextRetry = 0;
if (!isModeAvailable(MODE_GEOLOCATION)) {
currentOptimalModeIsGeolocation = false;
nextRetry = getNextRetryDelay();
log("Cannot switch to Geolocation because MODE_GEOLOCATION is not available.");
} else {
currentOptimalModeIsGeolocation = true;
nextRetry = 0;
log("Switching to Geolocation.");
}
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_LOCATION_FAILED, new Long(nextRetry));
if (restartTimer != null) {
restartTimer.cancel();
restartTimer = null;
}
restartTimer = new Timer();
restartTask = new RestartTask();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: Next attempt after " + (nextRetry / 1000) + " seconds.");
} else /** If Geolocation fails, retry after waiting an appropriate interval */
if ((mode == MODE_GEOLOCATION || //#ifdef BlackBerrySDK6.0.0
mode == MODE_GEOLOCATION_CELL || mode == MODE_GEOLOCATION_WLAN) && (((System.currentTimeMillis() - lastValidFixTime) / 1000) > geolocationTimeout)) {
switchToGPS = false;
retryAttempt++;
stopTracking();
long nextRetry = getNextRetryDelay();
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_LOCATION_FAILED, new Long(nextRetry));
if (restartTimer != null) {
restartTimer.cancel();
restartTimer = null;
}
restartTimer = new Timer();
restartTask = new RestartTask();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: Geolocation will be attempted after " + (nextRetry / 1000) + " seconds.");
} else /** If GPS fails, retry after waiting an appropriate interval */
if (mode == MODE_GPS && (((System.currentTimeMillis() - lastValidFixTime) / 1000) > gpsTimeout)) {
switchToGPS = false;
retryAttempt++;
stopTracking();
long nextRetry = getNextRetryDelay();
simpleLocationListener.locationEvent(SimpleLocationListener.EVENT_LOCATION_FAILED, new Long(nextRetry));
if (restartTimer != null) {
restartTimer.cancel();
restartTimer = null;
}
restartTimer = new Timer();
restartTask = new RestartTask();
lastValidFixTime = System.currentTimeMillis() + nextRetry;
restartTimer.schedule(restartTask, nextRetry);
log("Tracking: GPS will be attempted after " + (nextRetry / 1000) + " seconds.");
}
}
}
use of java.util.Timer in project blade by biezhi.
the class TaskKit method scheduleAtFixedRate.
/**
* 在指定的时间点开始以固定的频率运行任务。后续任务的启动时间不受前次任务延时影响。
* @param task 具体待执行的任务
* @param startTime 首次运行的时间点
* @param period 每次执行任务的间隔时间
* @param unit 时间单位
*/
public static void scheduleAtFixedRate(final Runnable task, Date startTime, final long period, final TimeUnit unit) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
taskScheduler.scheduleAtFixedRate(task, 0, period, unit);
timer.cancel();
timerList.remove(timer);
}
}, startTime);
timerList.add(timer);
}
use of java.util.Timer in project bigbluebutton by bigbluebutton.
the class KeepAliveService method start.
public void start() {
cleanupTimer = new Timer("keep-alive-task", true);
task = new KeepAliveTask();
cleanupTimer.scheduleAtFixedRate(task, 5000, runEvery);
}
Aggregations