Answer the question
In order to leave comments, you need to log in
How to immediately change the time for the handler after changing it in the settings?
Good afternoon. In general, the essence of the problem is as follows - there is a "pomodoro" timer, in it you can change the time in the settings. But the problem is that if, for example, the rest time is over, then I cannot change the working time, it only changes the next time. How can I make the time change immediately? Thanks)
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private final String TAG = "PomodoroTimer count";
SharedPreferences sharedPreferences;
private TextView mTimer;
private Button startButton, pauseButton;
int TIME_WORK_SESSION;//время работы(в секундах)
int TIME_REST_SESSION;//время отдыха(в секундах)
int BIG_REST_SESSION;
private int seconds;
private boolean isRunning;
private boolean isWork;
private boolean timeStop, workOrRest;
private boolean textSwitcher = true;
private int bigTomato = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTimer = (TextView) findViewById(R.id.tv);
startButton = (Button) findViewById(R.id.startButton);
pauseButton = (Button) findViewById(R.id.pauseButton);
startButton.setOnClickListener(this);
pauseButton.setOnClickListener(this);
seconds = TIME_WORK_SESSION;
isWork = true;
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
seconds= Integer.parseInt(String.valueOf(sharedPreferences.getString("work_time", "5")));
//Скрыть Action bar
//getSupportActionBar().hide();
runTimer();
}
@Override
protected void onResume() {
TIME_WORK_SESSION = Integer.parseInt(String.valueOf(sharedPreferences.getString("work_time", "5")));
TIME_REST_SESSION = Integer.parseInt(String.valueOf(sharedPreferences.getString("short_break", "2")));
BIG_REST_SESSION = Integer.parseInt(String.valueOf(sharedPreferences.getString("big_short", "7")));
super.onResume();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startButton:
textSwitcher = false;
timeStop = true;
isRunning = true;
break;
case R.id.pauseButton:
isRunning = false;
}
}
//Настройка тайера
private void runTimer() {
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int secs = seconds % 60;
String time = String.format("%d:%02d:%02d", hours, minutes, secs);
if (textSwitcher) {
if (workOrRest)
mTimer.setText("Rest");
else mTimer.setText("Work");
}
else
mTimer.setText(time);
if (timeStop) {
if (isRunning && isWork && seconds == 0) {
int rest;
isWork = false;
if (bigTomato == 3) { //Уход на большой перерыв
rest = BIG_REST_SESSION;
bigTomato = -1;
} else //Уход на меленький перерыв
rest = TIME_REST_SESSION;
seconds = rest;
textSwitcher = true;
workOrRest = true;
timeStop = false;
} else if (isRunning && !isWork && seconds == 0) {
isWork = true;
seconds = TIME_WORK_SESSION;
timeStop = false;
bigTomato += 1;
Log.d(TAG, "big Tomato count = " + bigTomato);
workOrRest = false;
textSwitcher = true;
} else if (isRunning) {
seconds--;
}
}
handler.postDelayed(this, 1000);//handler.post(this);
}
});
}
//Создание меню настроек
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add(0, 1, 0, "Setting");
menuItem.setIntent(new Intent(this, Prefs.class));
return super.onCreateOptionsMenu(menu);
}
}
public class Prefs extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Answer the question
In order to leave comments, you need to log in
1. Highly recommend using jodatime
2. Use broadcastreceiver
<receiver android:name=".receivers.StartupReceiver" >
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
<action android:name="ANDROID.INTENT.ACTION.DATE_CHANGED" />
</intent-filter>
</receiver>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question