How Do I Restart A Service?

时间:2018-08-22 13:55:33

标签: java android android-service

I have a service that I want to be stopped and then started fresh whenever a user hits a specific button, to which I have:

    stopService(new Intent(getBaseContext(), TimerService.class));
    startService(new Intent(getBaseContext(), TimerService.class));

However, I am under the impression that this is actually creating several services every time I hit the button. How do I start and stop the same service without creating duplicates?

EDIT: The items suggested unfortunately do not work for me. Upon attempting to implement the given answer it unfortunately does not change anything. My initial timer logs when its ticking and unfortunately the ticking is still occuring as the second timer, which is nested into the onFinish function is still yet to occur. So the services must not be stopped.

1 个答案:

答案 0 :(得分:0)

我相信您的问题可能在于使用RDOMail.Importcheck out this answer。我在将基本上下文与服务结合使用时遇到了问题。另外,检查您的意图,您正在使用停止服务器创建新的意图,而不是使用参考服务来停止它。 Check out this answer as well.

尝试:

getBaseContext()