1.Activityçåºç¡ç¥è¯(ä¸)
2.Android N å大ç»ä»¶çå·¥ä½åç
Activityçåºç¡ç¥è¯(ä¸)
ä¸ç¯æ»ç»äºActivityçä¸äºç¥è¯,苹果cms采集站源码ç°å¨ç»§ç»å¯¹Activityçç¥è¯è¿è¡æ¢³ç,å æ¬Activityç´æ¥ä¼ éæ°æ®,Activityççå½å¨æ,Activityçå¯å¨æ¨¡å¼ç.
1.intentä¼ éæ°æ®:
使ç¨startActivityæ¹æ³,intentçputExtra()æ¹æ³,以é®å¼å¯¹çå½¢å¼ä¼ éæ°æ®,该æ¹æ³æå¾å¤éè½½æ¹æ³,å¯ä»¥æ ¹æ®ä¼ éæ°æ®çä¸åç±»åéæ©åéçæ¹æ³.é¤äºæputExtra()æ¹æ³å¤,è¿æputExtras()æ¹æ³,ä¼ éçåæ°æ¯Bundle.
å¦æä¼ éçæ¯å¯¹è±¡,è¿ä¸ªå¯¹è±¡è¦å®ç°åºåå,ä¹å°±æ¯å®ç°Parcelableæè Serializableæ¥å£.
å¦æå¸æ被å¯å¨ç页é¢è¿åæ°æ®,éè¦ä½¿ç¨startActivityForResult()æ¹æ³,è¿ä¸ªæ¹æ³ä¸éè¦è®¾ç½®è®¿é®å·,ç¨æ¥åºåä¸åç访é®è .并ä¸å¨å¯å¨é¡µéåonActivityResultæ¹æ³ç¨æ¥æ¥æ¶è¿åçæ°æ®,
2.两ç§æ åµä¸çActivityççå½å¨æ.
æ£å¸¸æ åµä¸ççå½å¨æ,æ£å¸¸æ åµæ¯æç¨æ·çæ£å¸¸æä½ä¸çActivityççå½å¨æ.åé¢ä¼åæå¼å¸¸æ åµä¸ççå½å¨æ.
onCreate: Activity第ä¸æ¬¡å建æ¶åçåè°,主è¦æ¯å¨è¿ä¸ªæ¹æ³è¿è¡åå§åå·¥ä½,æ¯å¦åå§åæ§ä»¶åäºä»¶ç»å®å·¥ä½.
onStart:Activityä»ä¸å¯è§ç¶æåæå¯è§ç¶æ.
onResume:Activityåæåå°,å¯ä»¥åç¨æ·äº¤äº.
onPause:Activityå¯è§ä½æ¯ä¸è½åç¨æ·äº¤äº.
onStop:Activityä»å¯è§åå¾ä¸å¯è§,æ为åå°Activity.
onDestroy:Activityéæ¯æ¶è°ç¨.
onRestart:Activityä»åå°åæåå°Activity.
å¨å¯å¨Activityå两个Activityä¹é´è·³è½¬æ¶,å¯ä»¥ç¥éActivityççå½å¨æååè¿ç¨,æ两个说æ:(1)å¨ä¸¤ä¸ªActivity跳转æ¶,第ä¸ä¸ªActivtyçonPause,onStopæ¹æ³å第äºä¸ªActivityççå½å¨ææ¹æ³è°ç¨æ¶æº.
第ä¸ä¸ªActivityå æ§è¡onPauseæ¹æ³,第äºä¸ªActivityæè½å建.è¿ä¹å°±æå³çå¨onPauseæ¹æ³ä¸ä¸è½æ§è¡å¤ªèæ¶çæä½,å¦åä¼å½±å第äºä¸ªActivityçå建.å¨æºç (ActivityStack)ä¸æè¿æ ·ç注é:
(2)onStartåonResume,onPauseåonStopè¿ä¸¤å¯¹æ¹æ³çå®è´¨ä¸åå¤:onStartåonStopè¿ä¸¤ä¸ªæ¹æ³æ¯ä»Activityçå¯è§æ§æ¥åºåç,onResumeåonPauseæ¯ä»Activityæ¯å¦å¤äºåå°,æ¯å¦å¯ä»¥åç¨æ·äº¤äºæ¥åºåç,注æå¨onPauseè°ç¨æ¶Activityè¿æ¯å¯è§ç,è°ç¨æ¶æºæ¯å¦å¼¹åºdialogæ¶,ä¸é¢çActivityæ¯å¯è§ç.è¿ä¸ªæ¶åè°ç¨çæ¯onPauseæ¹æ³.
å¼å¸¸æ åµä¸Activityççå½å¨æ:å¼å¸¸æ åµä¸æ¯æèµæºç¸å ³é ç½®åçååæåå°Activity被系ç»åæ¶æ¶Activityççå½å¨æ.åå°Activity被系ç»åæ¶çæ åµæ¯è¾é¾å¤ç°,å¨èµæºç¸å ³é ç½®åçååæ¶ååå°Activity被åæ¶æ¶ççå½å¨ææ§è¡è¿ç¨æ¯ä¸æ ·ç,æ¯è¾å®¹æå¤ç°çå°±æ¯æ¨ªç«å±åæ¢æ¶ççå½å¨ææ§è¡è¿ç¨.å¨ AndroidManifestçActivityç»ä»¶ä¸é ç½®android:screenOrientationæ ç¾,å½è®¾ç½®å¯ä»¥æ¨ªç«æ¹åéçæ¹åæåºå¨æ¥è°èæ¶,å¨åæ¢æ¶ä¼åºç°å éæ¯Activityåå建çè¿ç¨.
è¿ç¨:
å¨è¿ç§æ åµä¸æå¯è½ä¼ææ°æ®ç丢失,ç³»ç»æä¾ç¨æ¥ä¿åæ°æ®åè¿åæ°æ®çæ¹æ³:onSaveInstanceStateåonRestoreInstanceState.ç¨æ¹æ³åæ°Bundleå¯ä»¥ä¿ååè¿åæ°æ®.
å¯ä»¥æ ¹æ®éè¦è®¾ç½®android:screenOrientationæ ç¾,设å®activityçæ¹å,å¦æactivityçæ¹åæ¯éè¦æ¨ªç«å±åæ¢,ä½æ¯ä¸å®¹è®¸éæ¯Activity,å¯ä»¥è®¾ç½®å¦ä¸æ ç¾,å½è¿äºæ åµ(常ç¨ç)åçååæ¯ä¸ä¼éæ°èµ°Activityççå½å¨ææ¹æ³,åªä¼è°ç¨onConfigurationChanged,å¯ä»¥æ ¹æ®æ åµå¨è¿ä¸ªæ¹æ³éæ´æ°æä½.
åæ¢æ¶çlogè¾åº
3.Activityçåç§å¯å¨æ¨¡å¼
æ å:æ¯Activityçé»è®¤å¯å¨æ¨¡å¼,对äºAndroidManifestçActivityèç¹ä¸çandroid:launchMode="standard"æ ç¾.
ç¹å®:æ¯æ¬¡å¯å¨é½ä¼éæ°å建æ°çActivity.
singleTop:对åºçAndroidManifesçActivityèç¹ä¸çandroid:launchMode="singleTop"æ ç¾
ç¹ç¹:å½æ¤æ¨¡å¼çActivityå¤äºæ 顶æ¶,ä¸ä¼éæ°å建æ°çActivity,ä¼è°ç¨onNewIntentæ¹æ³,å¦ææ´æ°Activityçintent,éè¦è°ç¨ setIntent()æ¹æ³,å ·ä½ççå½å¨æè¿ç¨
singleTask:å¨activityæ ä¸å·²ç»æéè¦åå¯å¨çactivityæ¶,ä¼å æ¸ é¤ä½äºéè¦å¯å¨activityä¹ä¸çactivity,ä¾å¦:å¯å¨é¡ºåºmainActivity-activityA -activityB-activityA,å ¶ä¸activityAæ¯singleTaskçå¯å¨æ¨¡å¼:
singleInstance:å¨ä¸ä¸ªæ ä¸åç¬åå¨çactivity.
å ³äºactivityæ :æ¯æç¨æ¥ç®¡çactivityä¸ç§"å è¿å åº"çéåç»æ,æ¥çactivity对åºæ çæ¹æ³:ActivityçgetTaskId()æ¹æ³,åä¸ä¸ªæ çidå¼æ¯ç¸åç.adb shell dumpsys activityå¨ç»ç«¯æ¥çæ ç»æ,æ¯å¦è¿æ¯ä¸é¢çactivityå¯å¨é¡ºåº,ä¸åæ¯ActivityAè¿æ¯è®¾ç½®æsingleInstance,è¿æ¯çæ ç»æ:æ两个TaskRecord,å ¶ä¸ActivityBåMainActivityä½äºåä¸ä¸ªæ ä¸.
4.Activityå¼åä¸ä½¿ç¨æå·§:
<1>å®ä¹ä¸ä¸ªç¶Activity,å¨å建æ°çActivityæ¶ç»§æ¿è¿ä¸ªactivityå³å¯,å°ä¸äºactivityçå ¬å ±è®¾ç½®å¯ä»¥è®¾ç½®å¨ç¶activityä¸,æ¯å¦è·åæ¯ä¸ªActivityçåå,设置activityççªä½å±æ§,åä¸ç®¡çactivityççå½å¨æç,
<2>å¨å¯å¨çactivityä¸å®ä¹éææ¹æ³,å¯å¨æ¡ä»¶ä¼æ¾èæè§:
<3>管çactivityç±»,ç¨æ¥ä¸é®éåºapp.å¨ç¶ç±»Activityçå建åéæ¯æ¶ç¨æ¥æ·»å å移é¤Activity,å¨éè¦ä¸é®éåºçå°æ¹è°ç¨éæfinishAllActivityæ¹æ³.
Android N å大ç»ä»¶çå·¥ä½åç
æ¬æ侧é讲解android N ç³»ç»ä¸å大ç»ä»¶çå·¥ä½åçï¼ä¸åç³»ç»åçç¥æå·®å«ãéè¿åæå大ç»ä»¶çå·¥ä½æµç¨å 深对Android Frameworkçç解ï¼ä¹ä¸ºæ件åå¼åæä¸åºç¡ã
Activity
å±ç¤ºä¸ä¸ªçé¢å¹¶åç¨æ·äº¤äºï¼å®æ®æ¼çæ¯ä¸ä¸ªåå°çé¢çè§è²ã
Service
计ç®åç»ä»¶ï¼ç¨äºåå°æ§è¡ä¸ç³»å计ç®ä»»å¡ï¼å·¥ä½å¨ä¸»çº¿ç¨ï¼èæ¶æä½éè¦å¦èµ·çº¿ç¨ï¼ å为å¯å¨ç¶æåç»å®ç¶æã
BroadcastReceiver
æ¶æ¯åç»ä»¶ï¼ä¸»è¦ç¨äºä¸åç»ä»¶æè ä¸ååºç¨ä¹é´çæ¶æ¯ä¼ éï¼å®å·¥ä½å¨ç³»ç»å é¨ï¼ä¸éåæ§è¡èæ¶æä½ï¼æä½è¶ è¿5sï¼ä¼åºç°ANRã
ContentProvider
æ°æ®å ±äº«åç»ä»¶ï¼ç¨äºåå ¶ä»ç»ä»¶æè åºç¨å ±äº«æ°æ®ï¼ä¸»è¦æ§è¡CURDæä½ã
æ们å¯å¨ä¸ä¸ªactivityæ两ç§æ¹æ³ï¼
第ä¸ç§ï¼Activityç´æ¥å¯å¨æ¹å¼ï¼ï¼
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
第äºç§ï¼Contextå¯å¨æ¹å¼ï¼
Intent intent = new Intent(this, MainActivity.class);
getApplicationContext().startActivity(intent);
ä¸åçå¯å¨æ¹å¼Activityçå·¥ä½æµç¨æç¹å·®å«ã
两ç§å¯å¨é½ä¼è°ç¨å°Instrumentationç±»ä¸çexecStartActivityçæ¹æ³ï¼ç³»ç»æç»æ¯éè¿ActivityThreadä¸çperformLaunchActivityå®æActivityçå建åå¯å¨ã
performLaunchActivityæ¹æ³ä¸»è¦å®æ以ä¸å·¥ä½ï¼
1ãéè¿ActivityClientRecord对象è·åå¯å¨activityçç»ä»¶ä¿¡æ¯
2ãéè¿mInstrumentation对象çnewActivityæ¹æ³è°ç¨classloaderå®æactivityçå建
3ãéè¿r.packageInfo(LoadedApk 对象)çmakeApplicationæ¹æ³å°è¯å建Application对象
4ãå建ContextImpl对象并è°ç¨Activityçattachæ¹æ³å®æä¸äºæ°æ®çåå§å
5ãè°ç¨ActivityçonCreateæ¹æ³
å¨Activityå¯å¨çè¿ç¨ä¸ï¼Appè¿ç¨ä¼é¢ç¹å°ä¸AMSè¿ç¨è¿è¡éä¿¡ï¼
Appè¿ç¨ä¼å§æAMSè¿ç¨å®æActivityçå½å¨æç管ç以åä»»å¡æ ç管çï¼è¿ä¸ªéä¿¡è¿ç¨AMSæ¯Server端ï¼Appè¿ç¨éè¿ææAMSçclient代çIActivityManagerå®æéä¿¡è¿ç¨ï¼
AMSè¿ç¨å®æçå½å¨æ管ç以åä»»å¡æ 管çåï¼ä¼ææ§å¶æ交ç»Appè¿ç¨ï¼è®©Appè¿ç¨å®æActivity类对象çå建ï¼ä»¥åçå½å¨æåè°ï¼è¿ä¸ªéä¿¡è¿ç¨ä¹æ¯éè¿Binderå®æçï¼Appæå¨server端çBinder对象åå¨äºActivityThreadçå é¨ç±»ApplicationThreadï¼AMSæå¨clientéè¿ææIApplicationThreadç代ç对象å®æ对äºAppè¿ç¨çéä¿¡ã
Serviceæ两ç§å¯å¨æ¹å¼ï¼startService()åbindService()ï¼ä¸¤ç§ç¶æå¯ä»¥å¹¶å:
startServiceæµç¨
bindServiceæµç¨
BroadcastReceiverçå·¥ä½è¿ç¨ä¸»è¦å æ¬å¹¿æç注åãåéåæ¥æ¶:
å¨æ注åè¿ç¨ï¼
åéè¿ç¨
éæ注åæ¯ç±PackageManagerServiceï¼PMSï¼å¨åºç¨å®è£ çæ¶åå®ææ´ä¸ªæ³¨åè¿ç¨çï¼é¤å¹¿æ以å¤ï¼å ¶ä»ä¸å¤§ç»ä»¶ä¹é½æ¯å¨åºç¨å®è£ æ¶ç±PMS解æ并注åçã
æ¯ä¸ªè¿ç¨çå ¥å£é½æ¯ActivityThead.main()ï¼Appçå¯å¨æµç¨å¦ä¸ï¼
ä»æºç ä¸å¯ä»¥çåºï¼
åºç¨å¯å¨çå ¥å£ä¸ºActivityThreadçmainæ¹æ³ï¼mainæ¹æ³ä¼å建ActivityThreadå®ä¾å¹¶å建主线ç¨æ¶æ¯éåã
attachæ¹æ³ä¸è¿ç¨è°ç¨AMSçattachApplicationæ¹æ³ï¼å¹¶æä¾ApplicationThreadç¨äºåAMSçéä¿¡ã
attachApplicationæ¹æ³ä¼éè¿bindApplicationæ¹æ³åHæ¥è°åActivityThreadçhandleBindApplicationï¼è¿ä¸ªæ¹æ³ä¼å å建Applicationï¼åå è½½ContentProviderï¼ç¶åæä¼åè°ApplicationçonCreateæ¹æ³ã
ç±ä¸å¾å¯ä»¥çåºï¼å¨ContentProviderçå¯å¨è¿ç¨ä¸ä¼´éçappè¿ç¨çå¯å¨ã
ContentProviderçå ¶ä»CURDæä½å¦insertï¼deleteï¼updateè·queryçæµç¨ç±»ä¼¼ã