为什么Service继承自Android中的Context?

Why Service inherits from Context in Android?

我正在研究 Javadoc 并注意到了这一点:

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

因此,在 Android 中,Service 似乎继承自 Context!

但这是什么意思呢?它们对我来说是完全不同的概念。

Service 和 Activity 都继承自 Context。 来自 Android 文档: "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."

与 Activity 一样,服务 class 需要能够获取资源并启动活动并执行所有需要访问应用程序级别 class 的事情。 实际上,服务很像 Activity,只是没有 UI 元素。

官方 android 文档描述 Context:

Interface to global information about an application environment. This 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.

Service作为

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

context其实是一个广义的称呼,在具体使用的时候可能会比较难理解。您可能想查看一个很好的解释 here.

服务从对象的继承可能更清楚,只是想象上下文是介于它们之间的东西,只是不像对象那么抽象。