1.Applicationä¸ç Context å Activity ä¸çContextåºå«
Applicationä¸ç Context å Activity ä¸çContextåºå«
Contextå¨æ们å¼åä¸ç»å¸¸ç¨å°ï¼ä¸ç®¡æ¯Frameworkæä¾ç»æ们çå大ç»ä»¶ï¼è¿æ¯åºç¨çº§å«çApplicationï¼è¿æ¯è´è´£è¡¨ç°å±çViewç¸å ³ç±»ï¼çè³è¿æ们å¾å¤æ¶åå建çå®ä½ç±»é½ä¼éè¦ææä¸ä¸ªContextçå¼ç¨ãé£ä¹Contextå°åºæ¯ä»ä¹å¢ï¼å»ºè®®çè¿ä¸ªï¼ /p/bde4cb
Contextè±æéä¹æ¯å½åä¸ä¸æï¼æè å½ååºæ¯ä¸ï¼
å®æ¹ææ¡£ï¼Context
public abstractclass Context extends Object
Interface to globalinformation about an application environment. This 源码怎么用debug源码is an abstract class whoseimplementation is provided by the Android system. It allows access toapplication-specific resources and classes, as well as up-calls forapplication-level operations such as launching activities, broadcasting andreceiving intents, etc.
ç±å®æ¹ææ¡£ï¼æ们å¯ä»¥ç¥éï¼
1.该类æ¯ä¸ä¸ªæ½è±¡ï¼abstract classï¼ç±»ï¼
2.å®æè¿°çæ¯ä¸ä¸ªåºç¨ç¨åºç¯å¢çä¿¡æ¯ï¼å³ä¸ä¸æï¼
3.éè¿å®ï¼Contextï¼æ们å¯ä»¥è·ååºç¨ç¨åºçèµæºåç±»ï¼ä¹å æ¬ä¸äºåºç¨çº§å«çæä½ï¼ä¾å¦ï¼å¯å¨ Activity,广æåæå¡çï¼;
åé¢æä»¬è®²è¿ Context æ¯ä¸ä¸ªæ½è±¡ç±»ï¼éè¿ Contextæ们å¯ä»¥è·ååºç¨ç¨åºçèµæºåç±»ï¼è°ç¨å®ä»¬çæ¹æ³ï¼é£ä¹å ·ä½å®ä¹çæ¹æ³æåªäºå¢ï¼æ们æ¥çä¸ä¸ Context çæºç ï¼
æºç éçæ¹æ³å¤ªå¤äºï¼æ»å ± è¡ãæ们ä»ä»¥ä¸é¨åæºç çå°äºçæç对象---ApplicationãActivityãServiceãBroadcastãè¿äºå¯¹è±¡å Context çå ³ç³»å°åºæ¯ä»ä¹å¢ï¼æ们çä¸ä¸å®æ¹ææ¡£å¯ç¥ï¼
1.Acitiivity 继æ¿èªContextThemeWrapper--->å继æ¿ContextWrapper--->Contextã
2.Appliction ãService继æ¿èªContextWrapper--->å继æ¿Contextã
3.ApplicationãService å Activity æç»é½æ¯ç»§æ¿èªContextï¼æ以å®ä»¬æ¯åä¸ä¸ªä¸ä¸æã
éè¿ä»¥ä¸ç继æ¿å ³ç³»ï¼æ们就å¯ä»¥ç¥éï¼Contextçå ·ä½ä½ç¨ä¼å æ¬ï¼
- å¯å¨ä¸ä¸ªæ°çActivity
- å¯å¨ååæ¢Service
- åé广ææ¶æ¯(Intent)
- 注å广ææ¶æ¯(Intent)æ¥æ¶è
- å¯ä»¥è®¿é®APKä¸åç§èµæºï¼å¦ResourcesåAssetManager
- å建View
- 访é®Packageçç¸å ³ä¿¡æ¯
- APKçåç§æé管ç
ç±ä¸é¢åæç继æ¿å ³ç³»ï¼æ们å¯ä»¥ç¥éï¼Contextå建çæ¶æºæä¸ä¸ªï¼
â å建Application 对象æ¶ï¼ èä¸æ´ä¸ªAppå ±ä¸ä¸ªApplication对象ï¼
â¡å建Service对象æ¶ï¼
â¢å建Activity对象æ¶ï¼
æ以åºç¨ç¨åºAppå ±æçContextæ°ç®å ¬å¼ä¸ºï¼
Serviceä¸ªæ° + Activityä¸ªæ° + 1ï¼Application对åºçContextå®ä¾ï¼
å¦ä¸ï¼Androidä¸contextå¯ä»¥ä½å¾å¤æä½ï¼ä½æ¯æ主è¦çåè½æ¯å è½½å访é®èµæºãå¨androidä¸å¸¸ç¨çcontextæ两ç§ï¼ä¸ç§æ¯application contextï¼ä¸ç§æ¯activity contextï¼é常æ们å¨åç§ç±»åæ¹æ³é´ä¼ éçæ¯activity contextã
两è çåºå«ï¼
thisæ¯Activity çå®ä¾ï¼æ©å±äºContextï¼å ¶çå½å¨ææ¯Activity å建å°éæ¯ãgetApplicationContext()è¿ååºç¨çä¸ä¸æï¼çå½å¨ææ¯æ´ä¸ªåºç¨ï¼åºç¨æ§æ¯å®æ被æ§æ¯ãActivity.thisçcontext è¿åå½åactivityçä¸ä¸æï¼å±äºactivity ï¼activityæ§æ¯æ¶è¢«æ§æ¯ã
使ç¨Contextæ¶æéè¦æ³¨æçä¸ä¸ªç¹å°±æ¯ï¼ä½¿ç¨äºä¸æ£ç¡®çcontextï¼æ¯å¦æä¸ä¸ªå ¨å±çæ°æ®æä½ç±»ç¨å°äºcontextï¼è¿ä¸ªæ¶åå°±è¦getApplicationContext èä¸æ¯ç¨ACtivityï¼å¦æå¨è¿ä¸ªå ¨å±æä½ä¸å¼ç¨çæ¯Activityçcontextï¼é£ä¹å°±ä¼ä¸ç´å¼ç¨Activityçèµæºï¼å¯¼è´GCæ æ³åæ¶è¿é¨åå åï¼ä»èæç»å¯¼è´äºå åæ³æ¼ã
å åæ³æ¼æ¯å¼åä¸å¸¸è§çé误ä¹ä¸ï¼è½ä¸è½åç°åå³äºå¼åè çç»éªï¼å½ç¶äºæ们ä¹ä¼ä¾èµç°æçå åæ³æ¼åºï¼ä½æ¯å¦ææ们å¨å¼åçæºå¤´åå°å åæ³æ¼çæ¦çï¼é£ä¹åæçå·¥ä½ä¼å°å¾å¤ã
以ä¸æ¯é¿å contextç¸å ³çå åæ³é²ï¼ç»åºçå ç¹å»ºè®®ï¼
以ä¸ç表å举çæ¯ä¸ç§Context对象ç对åºä½¿ç¨åºæ¯ï¼
ä»è¡¨ä¸å¯ä»¥çå°ï¼åUIç¸å ³çé½ä½¿ç¨ActivityçContext对象ã
å°ç»ï¼å¦ä¸åæï¼Contextå¨å¯¹åºå¼åéçæ¥æºå°±æ¯ä¸ä¸ªââActivityãServiceåAppliactionï¼é£ä¹æ们该å¦ä½éæ©ä½¿ç¨åªä¸ä¸ªContext对象å¢ï¼ä¸ä¸ªæ¯è¾ç®åçæ¹æ³æ¯ï¼å½ä½ æ æ³ç¡®å®ä½¿ç¨æ个Context对象æ¯å¦ä¼é æé¿å¼ç¨å¯¼è´å åæ³æ¼æ¶ï¼é£ä¹å°±ä½¿ç¨AppliactionçContext对象ï¼å 为Appliactionåå¨äºæ´ä¸ªåºç¨ççå½å¨æå ã
å¨å®é å¼åä¸ï¼æ们å¾å¾ä¼ä¸ºé¡¹ç®å®ä¹ä¸ä¸ªApplictaionï¼ç¶åå¨AndroidMainfest.xmlæ件ä¸è¿è¡æ³¨åï¼
èä¸å¨èªå®ä¹Applicationå¾å¾ä¼å®ä¹å¥½ä¸ä¸ªéææ¹æ³ï¼ç¨ä»¥å ¨å±è·åapplicationå®ä¾ï¼
ActivityåApplicationé½æ¯Contextçåç±»ï¼ä½æ¯ä»ä»¬ç»´æ¤ççå½å¨æä¸ä¸æ ·ãåè ç»´æ¤ä¸ä¸ªAcitivityççå½å¨æï¼æä»¥å ¶å¯¹åºçContextä¹åªè½è®¿é®è¯¥activityå çåç§èµæºãåè åæ¯ç»´æ¤ä¸ä¸ªApplicationççå½å¨æã
1.å¦ä½å¤æcontextæ¯å±äºåªä¸ªactivityï¼
2.å ¨å±ä¸åå¦ä½è·å对åºçcontextï¼
éæå è½½ä¸ä¸ªFragmentï¼å¨onCreateView()æ¹æ³ä¸éè¿getActivityè·åä¸ä¸æå®ä¾ï¼
3.å大ç»ä»¶å¯ä»¥åæ®éJavaç±»ä¸æ ·ï¼éç¨newçæ¹å¼å®ä¾ååï¼
Androidç¨åºä¸åJavaç¨åºä¸æ ·ï¼é便å建ä¸ä¸ªç±»ï¼å个main()æ¹æ³å°±è½è¿è¡ï¼Androidåºç¨æ¨¡åæ¯åºäºç»ä»¶çåºç¨è®¾è®¡æ¨¡å¼ï¼ç»ä»¶çè¿è¡è¦æä¸ä¸ªå®æ´çAndroidå·¥ç¨ç¯å¢ï¼å¨è¿ä¸ªç¯å¢ä¸ï¼ActivityãServiceçç³»ç»ç»ä»¶æè½å¤æ£å¸¸å·¥ä½ï¼èè¿äºç»ä»¶å¹¶ä¸è½éç¨æ®éçJava对象å建æ¹å¼ï¼newä¸ä¸å°±è½å建å®ä¾äºï¼èæ¯è¦æå®ä»¬åèªçä¸ä¸æç¯å¢ï¼ä¹å°±æ¯æ们è¿é讨论çContextãå¯ä»¥è¿æ ·è®²ï¼Contextæ¯ç»´æAndroidç¨åºä¸åç»ä»¶è½å¤æ£å¸¸å·¥ä½çä¸ä¸ªæ ¸å¿åè½ç±»ã