如何有效地讓應用程序停止時在後臺運行計時器?儀式現在我沒有做任何事情,計時器將繼續運行,當應用程序已經停止,但有些時候它會停止運行,沒有我給它的命令。 這就是我如何運行目前我的定時器:如何在後臺運行計時器
if(t == null){
t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if(DHPDPS==0){
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){
money = (DOTPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
} else{
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}
counter++;
//if(counter == 3000)
// t.cancel();
// Display pay per second
if(counter <= DHPDPS || DHPDPS == 0){
t2.setText("Your pay per second is: $"+result);
}else{
t2.setText("Your pay per second is: $"+result2);
}
}
});
}
}, 20, 20);
它在OnCreate()宣佈,謝謝!
你能告訴我一個我如何使用IntentService的例子嗎?我從來沒有處理過他們 – Hockeyman271 2012-08-07 23:20:32
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/ – Quinma 2012-08-07 23:24:02
那麼通過谷歌搜索「android intentservice示例」,你應該找到很多。這很簡單:只需擴展IntentService並覆蓋1或2個方法,在這些方法中,您可以執行之前在其他地方所做的工作。然後使用startService()方法和IntentService的類從您的活動中啓動服務。 – kgautron 2012-08-07 23:25:25