views:

41

answers:

1

I have a class the extends relativelayout. This class is inflated and added to a view by an activity. This class needs information from a service that I've created. What I'd like to do is bind the class to the service on its own thread but I'm not sure how to do it. When I try and call bindservice in the class I get a "method is not applicable" error. If I run bindService from the calling activity's context it does accept it but I get all sorts of problems from that.

I could simply have the calling activity bind to the service and act as a go-between but that's extra work and I can't help but think there's a better solution. Plus I would like to be able to imbed this class wherever I want to and it will work as its own entity.

Thanks in advance

A: 

You can only bind to a Service from an Activity or other Context -- sorry.

CommonsWare
DOH! Not the answer I wanted but at least I know which way to go now. Thanks for your quick answer
gdolph
Maybe you can help me with a problem related to getting this working. I've created a controller class to handle connections to and from the service. This class will run in the activity itself. When I try and call bindservice(intent) from this class I still get the "method not applicable" error. How do I get bindservice to run?
gdolph
@gdolph: You have to call `bindService()` on the `Activity`. See here for a sample project demonstrating the use of `bindService()`: http://github.com/commonsguy/cw-android/tree/master/Service/WeatherPlus/
CommonsWare
Let me give you a bit more background. I have an activity which has 3 separate timers on it. Each of these timers needs to connect to its own timer service. I'm looking for a way to implement this without having to have 3 sets of service starters/binders, etc.
gdolph
@gdolph: Given the large number of timing technologies already in Android (`postDelayed()`, `TimerTask`, `AlarmManager`, etc.), why are you reinventing the wheel? Even assuming there is value in having a "timer service", you do not need three of them. You need one of them that tracks three timers.
CommonsWare
@CommonsWare: There is value in having a timer service as I can have a boot lister restart them when the phone starts, and have the service send notifications when the timer(s) are completed. The problem is not the services, the problem is connecting the service(s) to my timer class instances. Whether I have 1 service running 3 timers or 3 services running I have the same issue.
gdolph
@gdolph: From what I can tell, you need zero services. Use `AlarmManager`, please.
CommonsWare
@CommonsWare: Why would I use AlarmManager for a countdown timer? Will AlarmManager enable me to have a widget that displays the timer status on the phone's main screen? No.
gdolph
@gdolph: "Will AlarmManager enable me to have a widget that displays the timer status on the phone's main screen?" -- yes. But, if you want to keep three services in memory all of the time and get all those one-star ratings on the Market as a result, that's your decision. Either work out an implementation that does not require binding (e.g., just pass the data you need via `Intent` extras and use `startService()`) or you have to implement the binding stuff.
CommonsWare
@CommonsWare: Assume for one minute that I'm running services for a good reason. Is there a way for me to create a class that will bind to the service that I can re-use? All I'm looking for is a way to do that.
gdolph