I have a TextView for frequently updating the time, which works fine, but the problem is that I want to change the schedule time for different conditions, but I cannot change the schedule time. It is still installed at the initial stage.
private Long mPeriod = Long.parseLong("8000"); public void startTimer() { Logger.i("startTimer", "startTimer"); stopTimer(); mTimer = new Timer(); mTimerTask = new TimerTask() { @Override public void run() { // postInvalidate(); Logger.i("timer", "timer"); Thread th = new Thread(new Runnable() { @Override public void run() { Logger.i("thread", "thread"); Long ll = System.currentTimeMillis(); mRelativeTime = DateUtils.getRelativeTimeSpanString( Long.parseLong(mText + ""), ll, 0); Long diff = ll - Long.parseLong(mText + ""); // long diffSeconds = diff / 1000 % 60; long diffMinutes = diff / (60 * 1000) % 60; long diffHours = diff / (60 * 60 * 1000) % 60; Logger.d("diffMinutes", diffMinutes + ""); Logger.d("diffHours", diffHours + ""); if (diffMinutes == 0 && diffHours == 0) { Logger.i("5000", "5000"); mPeriod = Long.parseLong("5000"); } else if (diffMinutes < 60 && diffMinutes != 0 && diffHours == 0) { Logger.i("30000", "30000"); mPeriod = Long.parseLong("30000"); } else if (diffHours > 0) { Logger.i("600000", "600000"); mPeriod = Long.parseLong("600000"); } **// This line is not working... mTimer.scheduleAtFixedRate(mTimerTask, 0, mPeriod);** // Change text handler.sendEmptyMessage(0); } }); th.run(); } }; mTimer.scheduleAtFixedRate(mTimerTask, 0, mPeriod); } public void stopTimer() { if (mTimer != null) { mTimer.cancel(); mTimer = null; } }
source share