Search code examples
javaandroidandroid-serviceandroid-context

Why Service inherits from Context in Android?


I was studying a Javadoc and noticed this:

java.lang.Object
    android.content.Context
        android.content.ContextWrapper
            android.app.Service

So it seems, in Android, that Service inherits from Context!

But what is the meaning? They appear as totally separated concepts to me.


Solution

  • Both Service and Activity inherit from Context. From the Android Documentation: "The Context is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc."

    Like an Activity, the Service class needs to be able to get resources and to launch activities and do all the things that need access to application-level classes. Actually, a Service is quite like an Activity, only without the UI element.