- [
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()
最新推荐文章于 2024-01-08 06:00:00 发布