- [
View.getContext()][1]: Returns the context the view is currently running in. Usually the currently active Activity. [
Activity.getApplicationContext()][2]: Returns the context for the entire application (the process all the Activities are running inside
of). Use this instead of the current Activity context if you need a
context tied to the lifecycle of the entire application, not just the
current Activity.[
ContextWrapper.getBaseContext()][3]: If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().
getContext():是Activity 、Service所对应的Context.随着Activity 、Service创建而创建,销毁而销毁,在该Activity 、Service中可以用this代替,在匿名类中使用XXXXActivity.this代替。
getApplicationContext():时Application所对应的Context.贯穿于与整个应用程序生命周期。
getBaseContext():是指在别的Context中访问该类的Context时使用,一般不建议使用。
getApplicationContext()、getBaseContext()、getContext()
最新推荐文章于 2022-10-18 23:51:04 发布
本文详细解析了Android开发中Context的不同类型及其用途,包括View.getContext()、Activity.getApplicationContext()和ContextWrapper.getBaseContext()等,并对比了它们之间的区别。
17万+

被折叠的 条评论
为什么被折叠?



