0

I have a service class and inside a service class I have timer task class.

Can I pass an Intent from my service class to other class which is in the service Reason - I need to use alarm manager just to fire timer task class from service, I want my service always to be running

public class TestService extends Service 
{

// I need to pass an intent to trigger alarm from TestService to timeTaskUpdate

 private class timeTaskUpdate extends TimerTask{ }


}
Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87
user1810931
  • 638
  • 2
  • 11
  • 33
  • This question has been answered numerous times already: http://stackoverflow.com/questions/2566350/how-to-always-run-a-service-in-the-background – jsmith Dec 06 '12 at 16:13
  • That was for running services in background my question is - Can I pass an Intent from my service class to other class which is in the service and how? – user1810931 Dec 06 '12 at 16:18

1 Answers1

0

You can't send an Intent to a class that isn't an Android class. TimerTask is a Java class, not an Android class.

But why pass an Intent anyway? timeTaskUpdate is within the scope of TestService. You should be able to use a global variable within timeTaskUpdate. Intents are for communicating between Android components, which by definition are not in the same scope.

Having said this, you should be careful. Though I'm not familiar with TimerTask, it may run on a separate thread. If so, you should be sure that you handle concurrency.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
  • Yes, you'll need to post the updates to the UI thread using a handler. – Kristopher Micinski Dec 06 '12 at 19:25
  • I was using timer task and timer before for scheduling, but sometimes my timer task is killed itself so I want to change my code to alarm manager. SO I was thinking to have an alarm manager within my service class and call the other class which as the code to execute when my alarm manager is triggered – user1810931 Dec 07 '12 at 14:32