首页 > 如何理解android中的上下文对象(Context对象)?

如何理解android中的上下文对象(Context对象)?

初学android,有很多方法里都要用到上下文对象。不知道如何理解,是指该方法在哪个activity中起作用么?


Context是我们在编写Android程序经常使用到的对象,意思为上下文对象。 常用的有Activity的Context还是有Application的Context。Activity用来展示活动界面,包含了很多的视图,而视图又含有图片,文字等资源。

更多参考避免Android中Context引起的内存泄露


首先题主把context打成了content。Android Developers对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.

Context描述了应用程序环境的信息,通过context可以获取资源和调用一些操作比如toast、发送intent等等,类似Win32中的句柄。但与句柄不同的是,在Win32中每个资源都有句柄(例如对话框),但在android中context只有activity、application和service。可以理解为拥有一个context就可以访问这个实例对应的资源,以及有权用这个实例调用方法。

【热门文章】
【热门文章】