如何在应用程序停止后让计时器继续倒计时?
问题描述:
我有一个类中有一个方法,其中有一个计时器。当调用该方法时,计时器将启动并永久启动,但当应用程序完全关闭时,它会停止并在应用程序重新打开时不倒计数。有没有办法检查计时器是否在之前滴答滴答,并现在使用共享首选项或类似的东西打勾?我不知道。如何在应用程序停止后让计时器继续倒计时?
所以基本上我该如何使应用程序停止后,计时器继续计数?
方法下面
公众诠释shipAdd(){ 如果(计数器> = 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;
}
答
一个伟大的方式来做到这将被使用的服务。
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
}
}
在要启动服务的类中,使用此命令启动该服务。
在一个活动:
startService(new Intent(getApplicationContext(), CountDownTimer.class));
在片段
getActivity().startService(new Intent(getActivity(), CountDownTimer.class));
更新你的清单与该行
<service android:name=".CountDownTimer" ></service>
退房[服务](HTTP:/ /developer.android.com/guide/components/services.ht毫升) – aProperFox 2014-10-10 17:53:24
我只希望它在应用程序打开时(UI已打开) – 2014-10-10 18:19:35
你说你希望它在应用程序停止时运行? – aProperFox 2014-10-10 18:22:23