I want to start a Timer from a class which will keep on running until i stop from it from other class. For this, I have created class for timer and methods named startTimer and stopTimer as given below:
public class TimerTaskClass extends Application {
Timer timer = new Timer();
public static final int TIME_INTERVAL = 10000;
public void startTimer(final Context context) {
Log.d("Constants", "Timer Started");
timer.scheduleAtFixedRate(new java.util.TimerTask() {
@SuppressLint("DefaultLocale")
@Override
public void run() {
//Performing my Operations
}
}, 0, TIME_INTERVAL);
}
public void stopTimer() {
timer.cancel();
}
What i am doing is, I have two activity ClassA and ClassB
Class A{
onCreate(){
TimerTaskClass tmClass=new TimerTaskClass();
tmClass.startTimer();
}
}
Class B{
onCreate(){
TimerTaskClass tmClass=new TimerTaskClass();
tmClass.stopTimer();
}
}
But this is not stopping my timer which i have started from ClassA. How this can be done?