概述
Android程序和Java程序一個不同的地方在于Android程序不能僅僅通過一個main方法就啟動起來,他還需要特定的環(huán)境解幼,就是Context抑党,稱之為上下文環(huán)境。
Context類的繼承關(guān)系
- Context
- ContextImpl
- ContextWrapper
- Application
- Service
- ContextThemeWrapper
- Activity
可以看出Context一共有三種類型:Application书幕、Service新荤、Activity
Context的類型
Activity揽趾、Service台汇、還有Application
Context數(shù)量 = Activity數(shù)量 + Service數(shù)量 + 1
任何一個Context的實例,只要調(diào)用getApplicationContext()方法都可以拿到我們的Application對象篱瞎。
Context的用途
- 啟動四大組件
- 發(fā)送廣播
- 操作數(shù)據(jù)庫
- 彈出Dialog
- 獲取系統(tǒng)服務(wù)
- 獲取Resource資源
- 獲取包名等應(yīng)用信息
- 獲取主線程Looper
- …...