I am using Intent Service in my program. In the Intent Service, I have a timer task thread which runs every 15 mins. Since it runs ina new thread, there is no way to stop it other than from inside the thread itself, which is not possible in my situation. So, I want to be able to pass a reference of the timer object from the initial activity to the Intent Service. And using that refernce, can I start or stop the timertask?
How to do this?
I am using Intent Service in my program. In the Intent Service, I have
Share
That is a very bad idea.
First, it will not work. An
IntentServiceshuts down as soon asonHandleIntent()returns. What you are really doing is leaking a thread.Second, it requires this
Serviceto stay in memory all of the time, which is an anti-pattern in Android.Please use
AlarmManagerto send commands to yourIntentServiceevery 15 minutes, and get rid of your timer task.And, by doing this, you no longer have to worry about passing this sort of object around between an activity and a service.