Search code examples
androidserviceextend

Is it possible to extend a service that has extended an Intent Service in android?


I'm developing an app in which I use several different services. These services frequently contain the same methods. In some cases I can put these methods in another class, but in other cases they are dependent upon methods that can only be run in classes extending an activity or service.

Essentially, I want to minimize the amount of code (and therefore work and possible complications) by creating a base service containing several of these methods and then extending it in all other services.

I can't find much info online. Is this possible? If so, how do I go about it? I had a crack but it throws up an activity not found exceptions.

Thanks!


Solution

  • Take a look at this example on abstract classes. Though Effective Java preferes interfaces to abstract classes based on what you're describing it sounds like this is a case where an abstract class makes sense.

    An abstract class is usefull when there is a common set of features that class members will implement but the actual implemention is different. The classic example is the GraphicObject - all shapes need a draw method but how a square draws itself is different than a circle.

    Basicly the idea is to set up a framework comprised of all the methods and such that are shared by services that will extend your abstract class. You can then control the implementaion of those methods in each of the classes extending your abstract class. You can also add any additional methods needed for a specific service to that classes implemention.