shiguiyou / shiguiyou.github.io Goto Github PK
View Code? Open in Web Editor NEW个人博客,常年不更
Home Page: http://shiguiyou.cn
个人博客,常年不更
Home Page: http://shiguiyou.cn
Context is the bridge between components. You use it to communicate between components, instantiate components, and access components.
我们使用context去初始化Activity, Content Provider, BroadcastReceiver等等,我们也是用它获取资源resource或者文件系统。
Context作为安卓系统的一个入口点,比如去获取系统的WifiManager, Vibrator, and PackageManager. context.getSystemService(Context.WIFI_SERVICE)
Let’s agree that everything in Android is a component. Context is the bridge between components. You use it to communicate between components, instantiate components, and access components. I hope the definition is now clear.
I divide context types into two categories: UI Context
and Non-UI Context
.
In reality, only the ContextThemeWrapper is UI Context — which means Context + Your theme.
UI Context:
- Activity
- Instance of your activity (this)
- Fragment
- getContext() in Fragment
- View
- getContext() in View (if View was constructed using UI-Context)
Non-UI Context is allowed do almost everything UI-Context can do (bad design spotted). But as we pointed out above, you lose theming.
Non-UI Context
- Application instance as context
- Activity
- getApplicationContext() in Activity
- Broadcast Receiver
- Context received in broadcast receiver
- Service
- Instance of your service (this)
- getApplicationContext() in Service
- Context
- getApplicationContext() in Context instance
Summary
Context类本身是一个纯abstract类,它有两个具体的实现子类:ContextImpl和ContextWrapper。其中ContextWrapper类,如其名所言,这只是一个包装而已,ContextWrapper构造函数中必须包含一个真正的Context引用,同时ContextWrapper中提供了attachBaseContext()用于给ContextWrapper对象中指定真正的Context对象,调用ContextWrapper的方法都会被转向其所包含的真正的Context对象。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.