Reputation: 43
I have a class that has a method that has a timer inside it. When the method is called the timer starts and it goes on forever but when the app is completely closed it stops and does not count down when the app is reopened. Is there a way to check if the timer was ticking before and make it tick now with like shared preferences or something? I dont know.
So Basically how do i make a timer continue counting down after the app stopped?
method below
public int shipAdd() { if (counter >= addSpend) {
counter -= addSpend;
addSpend += addSpend;
counterPerSec +=addAmount;
addClick += addClick;
test++;
// The count down timer below
new TimerClass(addTime, 1000) {
public void onFinish() {
counter += addAmount;
this.start();
}
}.start();
} else if (counter < addSpend) {
}
return addSpend;
}
Upvotes: 0
Views: 319
Reputation: 140
A great way to do this would be using a service.
public class CountDownTimer extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
//Enter your timer code here
}
}
In the class you want to start the service,use this to start the service.
In an activity:
startService(new Intent(getApplicationContext(), CountDownTimer.class));
In a Fragment
getActivity().startService(new Intent(getActivity(), CountDownTimer.class));
Update your manifest with this line
<service android:name=".CountDownTimer" ></service>
Upvotes: 1