Ashwin
Ashwin

Reputation: 13537

Can I pass an object's reference from one activity to another in android?

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?

Upvotes: 0

Views: 427

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

In the Intent Service, I have a timer task thread which runs every 15 mins.

That is a very bad idea.

First, it will not work. An IntentService shuts down as soon as onHandleIntent() returns. What you are really doing is leaking a thread.

Second, it requires this Service to stay in memory all of the time, which is an anti-pattern in Android.

Please use AlarmManager to send commands to your IntentService every 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.

Upvotes: 2

Related Questions