1.startserviceԴ?源码candle源码解析?
2.startService å bindServiceçåºå«
3.Serviceçå¯å¨
4.androidç¼ç¨å¦ä½è®©ç¨åºåå°è¿è¡
5.Android N å大ç»ä»¶çå·¥ä½åç
startserviceԴ??
æè¿å¨å¼åè¿ç¨ä¸éå°ç¸å ³é®é¢ï¼å¨æ¤è®°å½è¿ç¨å解å³æè·¯ã
1 åçå·²ç»å®ç°åè½
Radioåºç¨
a çå¬å¼æºå¹¿æstartService
b ç¹åºç¨å¾æ åï¼bindServiceï¼å®ç°ä¸Serviceçéä¿¡
2 éè¦æ°å®ç°çåè½
å ¶ä»åºç¨æ¥æ¶å¼æºå¹¿æåè°ç¨Radioåºç¨ï¼å¯è½ä¼startActivityï¼å¯è½ä¼startService, 被æèµ·åï¼æ¢å¤éåºåçç¶æã
a æ¢å¤éåºåçç¶æå¨Serviceä¸å®ç°ï¼å¨Serviceä¸çonBindåonStartCommandä¸å¯ä»¥è·åIntentï¼
ä»intentä¸è·åæ è®°ä½ï¼å¦æéè¦æ¢å¤ç¶æï¼æ¢å¤è®°å½çbandåchannelã
b ä½æ¯å¤æè¿ç¨åªéè¦ä¸æ¬¡ï¼æ以èèæ¯å¨onStartCommandä¸è¿æ¯onBindä¸å该å¤æã
c æµè¯åç°ï¼startServiceæ¶ï¼å¦æServiceå°æªå¯å¨ï¼onBindåonStartCommandé½ä¼æ§è¡å°ï¼å¦æserviceå·²ç»å¯å¨ï¼
ååªæ§è¡onStartCommandã
d æ以å°å¤æé»è¾æ¾å¨äºonStartCommandä¸ã
e å¦å¤ä¸ç§æ åµï¼å ¶ä»åºç¨æèµ·æ¥çæ¯Activityï¼éè¦å°intentä¸çæ è®°ä½éç¥å°serviceï¼ä¹åæ¯ç¨çonBind,åæ ·çåå ï¼
å½serviceå·²ç»å¯å¨çæ åµä¸ï¼onBindä¸ä¼éæ°æ§è¡ï¼å¹¶ä¸ï¼bindæèµ·æ¥çserviceä¸ä¼èµ°å°onStartCommandã
f èå¦æå¨Activityæèµ·æ¥serviceçæ¶åï¼åªç¨startServiceçè¯ï¼ååªä¼èµ°onStartCommand,æ æ³å®ç°ActivityåServiceçéä¿¡ã
g æ以æåå®ç°ï¼å¨activityä¸å startServiceåbindServiceã
1 Serviceä¹æ¯è¿è¡å¨ä¸»çº¿ç¨
ä¸ä¸ªæå¡ï¼serviceï¼è¿è¡å¨ä¸»çº¿ç¨ä¸ï¼æå¡å¹¶ä¸å建èªå·±ç线ç¨ï¼ä¹ä¸å¨é离è¿ç¨ä¸è¿è¡ï¼é¤éä½ æå®ï¼ãè¿æå³çï¼å¦æä½ çæå¡è¦æ§è¡CPUè´¹æ¶æä½æé»å¡æä½ï¼ä½ éè¦å¨æå¡ä¸å建æ°ç线ç¨æ¥æ§è¡è¯¥æä½ã使ç¨å ¶ä»çº¿ç¨ï¼å¯ä»¥é¿å ANRé误ï¼ä¿è¯åºç¨ç主线ç¨å¯ä»¥ä¸ç¨æ·äº¤äºã
Serviceå¤ç¨äºä¸éè¦åç¨æ·è¿è¡äº¤äºçåå°ææ¾é³ä¹ï¼åå°ææ¾Radioçæ åµï¼å¦ææ¶å交äºï¼åªæ¯ä¸å¸æèæ¶ï¼å¯ä»¥ç¨threadã
2 startServiceåbindServiceçåºå«
a 交äº
éè¿startserviceæ¥å¯å¨ä¸ä¸ªserviceï¼å¯å¨åï¼serviceå¨åå°è¿è¡ãé常æ¥è¯´ï¼è¯¥serviceæ¯æ æ³è¿åç»æçã
å¯ä»¥å©ç¨bindserviceæ¥åserviceç»å®ï¼ç»å®åï¼æ们å¯ä»¥åservice交äºï¼åé请æ±ï¼å¾å°ç»æçè³æ§è¡IPCéä¿¡ã
b çå½å¨æ
startService
éè¿è°ç¨startServiceå¯å¨æå¡çè¿ç¨ï¼
-ãonCreate âãonStartCommand
å¤æ¬¡è°ç¨startServiceï¼æå¡ä¼å¤æ¬¡æ§è¡ï¼
-ãonStartCommand
å¤æ¬¡è°ç¨startServiceåï¼è°ç¨ä¸æ¬¡stopServiceå³å¯ç»ææå¡ãï¼è¥å¤æ¬¡è°ç¨stopServiceï¼åªæ第ä¸æ¬¡æç¨ï¼
è°ç¨stopServiceçæå¡ç»æè¿ç¨
-ãonDestroy
bindService
è°ç¨bindServiceå¯å¨æå¡çè¿ç¨ï¼
-ãonCreate âãonBind âãï¼onServiceConnectedï¼
å¤æ¬¡è°ç¨bindServiceï¼æå¡æ¬èº«æªæ§è¡ä»»ä½æä½ã
ä¸æ¬¡unBindServiceå°±è½ç»ææå¡ãï¼è¥å¤æ¬¡è°ç¨unBindServiceï¼ç¬¬ä¸æ¬¡æç¨ï¼åé¢ä¼åºéï¼
è°ç¨unBindServiceçæå¡ç»æè¿ç¨ï¼
-ãonUnbind âãonDestroy
å startServiceåbindService
å è°ç¨startServiceï¼åè°ç¨bindServiceãæå¡çæ§è¡è¿ç¨ä¸ºï¼
onCreate âãonStartCommand âãonStart âãonBind âãï¼onServiceConnectedï¼
å unBindServiceï¼åstopServiceãæå¡ç»æçæ§è¡è¿ç¨ï¼
onUnbind âãonDestroy
é注æçæ¯ï¼unBindServiceä¼æ§è¡å°onUnbindï¼stopServiceä¼æ§è¡å°onDestroyã
å stopServiceï¼åunBindServiceãæå¡ç»æçæ§è¡è¿ç¨ï¼
onUnbind âãonDestroy
é注æçæ¯ï¼stopServiceä¸ä¼æ§è¡ä»»ä½æä½ï¼unBindServiceä¼æ§è¡å°onUnbindâãonDestroyã
å bindServiceåstartService
å è°ç¨bindServiceï¼åè°ç¨startServiceãæå¡çæ§è¡è¿ç¨ä¸ºï¼
onCreate âãonBind âãï¼onServiceConnectedï¼ âãonStartCommand
å unBindServiceï¼åstopServiceã
æå¡æ§è¡çè¿ç¨å ä¸ã
å stopServiceï¼åunBindServiceãæå¡ç»æçæ§è¡è¿ç¨ï¼
æå¡æ§è¡çè¿ç¨å ä¸
æ»ç»ï¼
1ãå¤æ¬¡bindServiceæ¶ï¼æå¡æ¬èº«çonBindä¸ä¼è¢«å¤æ¬¡æ§è¡ã
2ãbindä¸ä¸ä¸ªServiceåï¼æ§è¡ä¸æ¬¡unBindServiceå°±å¤äºãä¸ç¶ä¼åºéã
3ãä¸ä¸ªAppéï¼åä¸ä¸ªActivityå¤æ¬¡bindä¸ä¸ªæå¡ï¼é¤äºç¬¬ä¸æ¬¡ï¼åé¢çbindä¸ä¼æä»»ä½onBindãonServiceConnectedæå°ã
ä¸ä¸ªAppéï¼ä¸åçActivityå»bindä¸ä¸ªæå¡ï¼ç¬¬ä¸æ¬¡bindæonBindãonServiceConnectedæå°ï¼åé¢çbindåªä¼
onServiceConnectedæå°ã
4ãä¸ä¸ªActivity bindä¸ä¸ä¸ªServiceåï¼å¦æActivity finishå没æè°ç¨unBindï¼Appä¼å´©æºï¼Logæå°å¦ä¸ï¼
android.app.ServiceConnectionLeaked: Activity com.example.testactivity1.MainActivity has leaked ServiceConnection
com.example.testactivity1.MainActivity$1@d that was originally bound here.
3 startForegroundService
Android 8.0 æä¸é¡¹å¤æåè½ï¼ç³»ç»ä¸å 许åå°åºç¨å建åå°æå¡ã å æ¤ï¼Android 8.0 å¼å ¥äºä¸ç§å ¨æ°çæ¹æ³ï¼å³ Context.startForegroundService()ï¼ä»¥å¨åå°å¯å¨æ°æå¡ã
å¨ç³»ç»å建æå¡åï¼åºç¨æäºç§çæ¶é´æ¥è°ç¨è¯¥æå¡ç startForeground() æ¹æ³ä»¥æ¾ç¤ºæ°æå¡çç¨æ·å¯è§éç¥ã
å¦æåºç¨å¨æ¤æ¶é´éå¶å æªè°ç¨ startForeground()ï¼åç³»ç»å°åæ¢æå¡å¹¶å£°ææ¤åºç¨ä¸º ANRã
8.0 以åä¸å¸æåå°åºç¨è¿è¡åå°æå¡ï¼é¤éç¹æ®æ¡ä»¶
ä¸æ¦éè¿startForegroundService() å¯å¨åå°æå¡ï¼å¿ é¡»å¨service ä¸æstartForeground() é å¥ï¼ä¸ç¶ä¼åºç°ANR æè crash
startForeground() ä¸çid ånotification ä¸è½ä¸º0 å null
startService å bindServiceçåºå«
1. çå½å¨æï¼
startService()æ¹å¼å¯å¨ï¼Serviceæ¯éè¿æ¥åIntent并ä¸ä¼ç»åonCreate()åonStart()ãå½ç¨æ·å¨ååº
æå¾ä½¿ä¹éæ¯æ¶ä¼ç»åonDestroy()ï¼èbindService()æ¹å¼å¯å¨ï¼ä¸Activityç»å®çæ¶åï¼ä¼ç»åonCreate()å
onBind()ï¼èå½Activity被éæ¯çæ¶åï¼Serviceä¼å è°ç¨onUnbind()ç¶åæ¯onDestroy()ã
2. æ§å¶æ¹å¼ï¼
åè çæ§å¶æ¹å¼éè¦ä½¿ç¨åºå®çæ¹æ³ï¼å¯¹Serviceè¿è¡åä¸çæä½ãèåè ç±äºä¸Activityç»å®ï¼ä¸ç¨èèå ¶çå½å¨æé®é¢ï¼å¹¶ä¸ä»åé
Intentç被å¨æä½ï¼å为å¯ä»¥ä¸»å¨å¯¹Service对象è¿è¡æä½ï¼æ们çè³å¯ä»¥å»ºç«ä¸ä¸ªHandlerç±»ï¼å¯¹Serviceè¿è¡ç¸å ³çæä½ã大大å 强
äºServiceççµæ´»æ§ãå¯æä½æ§ã
æ»ç»ï¼å¯¹äºç®åçåºç¨startService()å¯å¨æ¹å¼è½å¸¦æ¥æ´å°ç代ç ï¼ç®åçæä½ã对äºå¤æçåºç¨bindService()æ¹å¼ï¼è½ç¶å¸¦æ¥çæ´å¤çç¼ç ï¼ä½åæ¶ä¹å¸¦æ¥äºæ´å¥½çå¯æä½æ§ï¼ä½¿å ¶ä½¿ç¨èµ·æ¥æ´åActivityã