Reputation: 534
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?
Upvotes: 1
Views: 2940
Reputation: 13019
Your TimerTaskClass
extends Application
. This is possible although it may not the best solution in every case, see the documentation. To follow through with this approach, you also have to set the android:name
attribute for application
in the Manifest.xml
:
<application
android:name=".TimerTaskClass"
...keep the rest like before...
>
... keep this part unchanged as well ...
</application>
Now, TimerTaskClass
will be instanciated as soon as your app is launched.
You can access it from every Activity
like this:
TimerTaskClass ttc = (TimerTaskClass)getApplication();
The timer
will be created as member of the Application
instance (which is a Singleton by default), so there will only be one instance of it. So you can start it as follows:
ttc.startTimer(getApplicationContext());
If you decide that using your own Application
object is not what you want, I think the answer by @Thror will be the best way to go. BTW in this case, you certainly should not let TimerTaskClass
inherit from Application
- it would not be useful and might even cause problems.
Upvotes: 1
Reputation: 1793
For this you have to create singleton as below
public class TimerTaskClass {
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();
}
Now in Activity you have to do this
TimerTaskClass tmClass=new TimerTaskClass();
tmClass.startTimer();
And in Second Activity
TimerTaskClass tmClass=new TimerTaskClass();
tmClass.stopTimer();
Hope you understand the concept
Upvotes: 0
Reputation: 447
Maybe you could change TimerTaskClass to singleton, so in both activities you will use the same instance of TimerTaskClass, it should solve your problem.
public class TimerTaskClass
{
private static TimerTaskClass _instance;
private TimerTaskClass ()
{
}
public synchronized static TimerTaskClass getInstance()
{
if (_instance == null)
{
_instance = new TimerTaskClass ();
}
return _instance;
}
}
Upvotes: 2
Reputation: 4258
The problem is that Activity A and Activity B each create their own TimerTaskClass
instance. Instead, they should share an instance.
The easiest way to do this is to implement the TimerTaskClass
as a Singleton so that both activities can talk to the same instance of the class.
Beware that overuse of the Singleton pattern leads to bugs that can be hard to trace and fix. As long as there aren't many components invoking methods on your singleton at the same time, you should be safe.
Upvotes: 0