1. 说æ
androidçåå°è¿è¡å¨å¾å¤serviceï¼å®ä»¬å¨ç³»ç»å¯å¨æ¶è¢«SystemServerå¼å¯ï¼æ¯æç³»ç»çæ£å¸¸å·¥ä½ï¼æ¯å¦MountServiceçå¬æ¯å¦æSDå¡å®è£
å移é¤ï¼ClipboardServiceæä¾åªåæ¿åè½ï¼PackageManagerServiceæä¾è½¯ä»¶å
çå®è£
移é¤åæ¥çççï¼åºç¨ç¨åºå¯ä»¥éè¿ç³»ç»æä¾çManageræ¥å£æ¥è®¿é®è¿äºServiceæä¾çæ°æ®ï¼ä»¥ä¸å°è¯´æä»ä»¬çå·¥å
·æµç¨
2. 举ä¾è¯´æåºæ¬æµç¨
以androidç³»ç»æ¯æsensorï¼ä¼ æå¨ï¼å®ä¾æ¥è¯´ææ¡æ¶å±çserviceåmanageræ¯å¦ä½é
åå·¥ä½ç
1) ä»ä¹æ¯sensor
sensoræ¯ä¼ æå¨, æ¯å¦æ§å¶æ¨ªç«å±åæ¢å©ç¨çå°±æ¯éåä¼ æå¨(gsensor), è¿æaccelerator sensorå¯åå¾x, y, zä¸ä¸ªè½´ä¸çå é度(åºç¨å¦å¹³è¡¡ç, å°ç´åé¦èç)
2) åºç¨ç¨åºè°ç¨ï¼ä»¥ä¸ä¸ºå
³é®ä»£ç ï¼
sensorManager=(SensorManager)getSystemService(context.SENSOR_SERVICE);
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(sensorListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
3) Managerå±
a) æä¾ç»åºç¨ç¨åºè°ç¨çæ¥å£ï¼åå®ä¸Service交äºï¼å®ç°åè½
frameworks/base/core/java/android/hardware/SensorManager.java
4) Serviceå±
a) å¼æºæ¶å°±è¿è¡ç管çSensorçåå°æå¡
frameworks/base/services/java/com/android/server/SensorService.java
b) snesoråå°æå¡éè¦çJNIï¼éè¿å®ä¸ç³»ç»çº§äº¤äº
frameworks/base/services/jni/com_android_server_SensorService.cpp
5) ç³»ç»å±
a) ä¼ æå¨ç头æ件ï¼ç¡¬ä»¶æä¾åææ¤æ件çå®ä¹å®ç°å
¶åè½
hardware/libhardware/include/hardware/sensors.h
b) ä¼ æå¨çç³»ç»å±å®ç°ï¼ä¸å
æ ¸äº¤äºï¼æ¤å¤é常æ¯ç¡¬ä»¶æä¾åæä¾ç
hareware/libsensors
6) å
æ ¸å硬件å±
å
æ ¸è®¿é®ç¡¬ä»¶ï¼åæ¶ä»¥è®¾å¤æ件çæ¹å¼æä¾ç»ä¸å±æ§å¶æ¥å£åä¼ æå¨æ°æ®
3. ç³»ç»å±å®ç°
1) frameworks/base/core/java/android/*Manager.java 对åºç¨çæ¥å£
2) frameworks/base/core/jni/ 对åºç¨çæ¥å£çJNI
3) frameworks/base/services/java/com/android/server/ åå°æå¡
4) frameworks/base/services/jni/ JNIä¸ç³»ç»å±æ¥å£
5) hardware/libhardware/include/ ç³»ç»å±å¤´æ件
6) hardware/libxxx ç³»ç»åºæ¯æ
7) å
æ ¸æ¯æ
4. åºç¨ç¨åºå¦ä½ä½¿ç¨
1) æ¥çç³»ç»æä¾åªäºæå¡
find frameworks/base/core/java/android/ -name *Manager.java
æ¤å¤å¯ä»¥çå°è°ç¨ç³»ç»æä¾æå¡çå
¥å£
2) ä¸è¬register listenerï¼äºä»¶åçæ¶é½æ¶å°åè°
5. æ°å»ºä¸ä¸ªserviceï¼ä»¥froyo为ä¾ï¼
1) æ¥å£ï¼æ¥å£ä¾åºç¨è°ç¨
frameworks/base/core/java/android/app/ContextImpl.java å æå¡åä¸Manager对åº
frameworks/base/core/java/android/content/Context.java å æå¡åå®ä¹
2) Managerï¼æä¾æå¡å¯¹åºçè°ç¨æ¥å£
frameworks/base/core/java/android/app/StartXXXXManager.java å®ç°è°ç¨æ¥å£
frameworks/base/core/java/android/app/IXXXXManager.aidl å®ä¹è°ç¨æ¥å£
frameworks/base/Android.mk å å
¥aidlçç¼è¯
3) serviceï¼æä¾åå°æå¡æ¯æ
frameworks/base/services/java/com/android/server/XXXXService.java æå¡å®ç°
frameworks/base/services/java/com/android/server/SystemServer.java å¯å¨æå¡
温馨提示:答案为网友推荐,仅供参考