[英]onDestroy being called but service not ending
I'm following the book 'Beginning Android 4 Development', and I'm controlling a service using the following functions from buttons: 我正在阅读“开始Android 4开发”一书,我正在使用按钮中的以下功能来控制服务:
public void startService(View view) {
startService(new Intent(getBaseContext(), QOLService.class));
}
public void stopService(View view) {
stopService(new Intent(getBaseContext(), QOLService.class));
}
QOLService.java includes QOLService.java包括
public class QOLService extends Service {
int counter = 0;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Keep running service until stopped, so return sticky
Timer timer=new Timer();
TimerTask tt =new TimerTask() {
@Override
public void run() {
Log.d("QOLService", String.valueOf(++counter));
}
};
timer.scheduleAtFixedRate(tt, 0, 1000);
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
}
As intended, on pressing the start button I get the 'service started' toast, and in logcat I get a message incrementing every second. 按照预期,在按下开始按钮时,我得到'service started'toast,在logcat中,我得到一条消息每秒递增一次。 This continues, as intended, even when the application is closed.
即使应用程序关闭,这仍将按预期继续进行。
When I click the stopservice button, I also get the expected 'service destroyed' message, but the timer lives on! 当我点击停止服务按钮时,我也得到了预期的“服务销毁”消息,但计时器依然存在! If I close the application it still keeps going.
如果我关闭应用程序,它仍然继续。 If I click the stopservice button again, it does NOT given the service destroyed message, as if it had been successfully destroyed the first time.
如果我再次单击stopservice按钮,它不会给出服务销毁消息,就好像它第一次被成功销毁一样。
Am I calling my timer inappropriately? 我不恰当地打电话给我的计时器吗? If so, I seem to be doing it exactly as the book advises!
如果是这样的话,我似乎正如本书所建议的那样做!
Am I calling my timer inappropriately?
我不恰当地打电话给我的计时器吗?
You are never stopping the timer. 你永远不会停止计时器。 Hence, it will keep running until the process is terminated.
因此,它将一直运行,直到该过程终止。 You should stop the timer in
onDestroy()
. 你应该在
onDestroy()
停止计时器。
I am agree with CommonsWare, You haven't stop your timer in the code. 我同意CommonsWare,你没有在代码中停止你的计时器。 I suggest you to go this way,
我建议你走这条路,
public class QOLService extends Service {
int counter = 0;
Timer timer;
TimerTask tt;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Keep running service until stopped, so return sticky
timer=new Timer();
tt =new TimerTask() {
@Override
public void run() {
Log.d("QOLService", String.valueOf(++counter));
}
};
timer.scheduleAtFixedRate(tt, 0, 1000);
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
tt.cancel();
timer.cancel();
}
}
The cancel() method will stop your Timer as well as. cancel()方法将停止你的Timer以及。
It might be because the system is trying to restart your service because you are returning START_STICKY
from onStartCommand
. 这可能是因为系统正在尝试重新启动您的服务,因为您从
onStartCommand
返回START_STICKY
。 Try returning START_NOT_STICKY
instead. 请尝试返回
START_NOT_STICKY
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.