I'm using a timer service and want to bind/unbind it on a button click in my main activiy. The issue is when I start my timer and exit the activity and come back and attempts to stop it, it doesn’t stop running.
my Timer service code
`public class TimerService extends Service {
private final IBinder binder = new LocalBinder();
private int second =0, minute=0, hour=0;
public static String seconds =null, minutes=null, hours=null, finalCounterValue=null;
public static Timer sessionTimer;
public class LocalBinder extends Binder {
TimerService getService() {
return TimerService.this;
}
}
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
startTimer();
return binder;
}
@Override
public void onDestroy() {
super.onDestroy();
sessionTimer.cancel();
Toast.makeCustomToast(TimerService.this, "Timer is stopped");
}
@Override
public boolean onUnbind(Intent intent) {
sessionTimer.cancel();
return super.onUnbind(intent);
}
private void startTimer() {
sessionTimer = new Timer();
sessionTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
seconds = String.valueOf(second);
minutes = String.valueOf(minute);
hours = String.valueOf(hour);
if (seconds.length() == 1) {
seconds = "0" + seconds;
}
if (minutes.length() == 1) {
minutes = "0" + minutes;
}
if (hours.length() == 1) {
hours = "0" + hours;
}
second++;
if (second == 59) {
second = 0;
minute++;
}
if (minute == 59) {
minute = 0;
second = 0;
hour++;
}
finalCounterValue = hours + ":" + minutes + ":" + seconds;
DashboardActivity.txtTime.setText(finalCounterValue);
Log.d(TAG, "Session timer:"+finalCounterValue);
}
}, 0, 1000);
}
}
`
My main activity this code is in OnCreate() start + stop will call the bind/unbind
`btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnStart = (Button) v;
if (btnStart.getText().equals("Pause")) {
btnStart.setText("Start");
btnStart.setBackgroundResource(R.drawable.rounded_red_button);
stopSessionTimer();
} else if (btnStart.getText().equals("Start")) {
startSessionTimer();
btnStart.setText("Pause");
btnStart.setBackgroundResource(R.drawable.rounded_pause_button);
}
}
});
`
My connection part in Mainactivity
`private final ServiceConnection timerConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
timerService = ((TimerService.LocalBinder)service).getService();
Toast.makeCustomToast(DashboardActivity.this,"Service connected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
timerService= null;
Toast.makeCustomToast(DashboardActivity.this,"Service diconnected");
}
};
void doBindService() {
if (bindService(new Intent(DashboardActivity.this, TimerService.class),
timerConnection, Context.BIND_AUTO_CREATE)) {
bound = true;
} else {
Log.e("MY_APP_TAG", "Error: The requested service doesn't " +
"exist, or this client isn't allowed access to it.");
}
}
void doUnbindService() {
if (bound) {
unbindService(timerConnection);
bound = false;
}
}
`
What I want to achieve is I want my main activity to control my timer service (bind/unbind) it on a button click even if I left my activity and came back. Right now it's working fine except if I click on START then leave and come back, I can't stop it.