系統啟動過程圖: 
Framework層所有的Service都是運行在SystemServer進程中;SystemServer進程是由Zygote進程建立。 
SystemServer進程啟動分兩個過程init1建立Service和進程狀態物件;init2建立Framework層的Service,將其加入到ServiceManager中,最後啟動launcher; 
Android提供了Watchdog類,用來監測Service是否處於正常工作中,是在SystemServer中啟動的。 
下面看一下SystemServer中Watchdog這個過程。 
SystemServer.java: 
複製代碼 代碼如下:public void run() { 
//初始化Watchdog 傳入各個Service作為參數 
Watchdog.getInstance().init(context, battery, power, alarm, 
ActivityManagerService.self()); 
//啟動Watchdog 
Watchdog.getInstance().start(); 
} 
Watchdog類實現 
類繼承結構: 
看到Watchdog是一個Thread,運行在SystemServer進程中,單例模式; 
HeartbeatHandler處理接受監控的對象(Service),運行在主線程中; 
Monitor提供監控介面,接受監控對象實現此介面; 
XXXService具體實現的檢測對象。 
執行流程: 
對外介面 
初始化: 複製代碼 代碼如下:public void init(Context context, BatteryService battery, 
PowerManagerService power, AlarmManagerService alarm, 
ActivityManagerService activity) { 
//儲存Service對象,運行在同一個進程中 
mResolver = context.getContentResolver(); 
mBattery = battery; mPower = power; 
mAlarm = alarm; mActivity = activity; 
//註冊廣播 
context.registerReceiver(new RebootReceiver(), 
new IntentFilter(REBOOT_ACTION)); 
mRebootIntent = PendingIntent.getBroadcast(context, 
, new Intent(REBOOT_ACTION), 0); 
…… 
//開機時間 
mBootTime = System.currentTimeMillis(); 
} 
註冊監控對象: 複製代碼 代碼如下:public void addMonitor(Monitor monitor) { 
synchronized (this) { 
//將監控對象加入到列表中 
mMonitors.add(monitor); 
} 
} 
搜尋一下此函數的調用,表示被監控;看到在如下Service中實現Watchdog的Monitor介面: 
ActivityManagerService 
InputManagerService 
NetworkManagementService 
PowerManagerService 
WindowManagerService 
都有調用:Watchdog.getInstance().addMonitor(this); 
Watchdog線程執行函數: 複製代碼 代碼如下:public void run() { 
boolean waitedHalf = false; 
while (true) { 
//監測完成標誌 
mCompleted = false; 
//發送監測訊息 
mHandler.sendEmptyMessage(MONITOR); 
synchronized (this) { 
long timeout = TIME_TO_WAIT; 
long start = SystemClock.uptimeMillis(); 
while (timeout > 0 && !mForceKillSystem) { 
//休眠等待檢查結果 
wait(timeout); // notifyAll() is called when mForceKillSystem is set 
timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start); 
} 
if (mCompleted && !mForceKillSystem) { 
//檢查結果OK 
waitedHalf = false; 
continue; 
} 
//在進行檢查一次 
if (!waitedHalf) { 
ActivityManagerService.dumpStackTraces(true, pids, null, null, 
NATIVE_STACKS_OF_INTEREST); 
waitedHalf = true; 
continue; 
} 
} 
//表明監控對象有問題 
// If we got here, that means that the system is most likely hung. 
// First collect stack traces from all threads of the system process. 
// Then kill this process so that the system will restart. 
//儲存stack資訊 
…… 
// Only kill the process if the debugger is not attached. 
if(!Debug.isDebuggerConnected()) { 
if(SystemProperties.getInt("sys.watchdog.disabled", 0) == 0) { 
//kill當前進程SystemServer 
Process.killProcess(Process.myPid()); 
System.exit(10); 
} 
} 
waitedHalf = false; 
} 
} 
在此run函數中迴圈發送訊息,判斷標誌是否正常,決定檢測對象是否正常工作。 
若監測對象不正常工作,則收集重要的stack資訊儲存下來,然後重啟SystemServer。 
監測訊息的處理: 
是在HeartbeatHandler中進行,看看訊息處理函數。 複製代碼 代碼如下:public void handleMessage(Message msg) { 
switch (msg.what) { 
case MONITOR: { 
// See if we should force a reboot. 
//監測對象是否正常工作中…… 
final int size = mMonitors.size(); 
for (int i = 0 ; i < size ; i++) { 
//調用監測對象的monitor介面 
mCurrentMonitor = mMonitors.get(i); 
mCurrentMonitor.monitor(); 
} 
//走到這裡表明監測對象正常 
synchronized (Watchdog.this) { 
mCompleted = true; 
mCurrentMonitor = null; 
} 
} break; 
} 
} 
判斷監測對象是否正常工作,通過調用監測對象實現的介面monitor,看看這個介面該如何執行的。 
PowerManagerService中: 
public void monitor() { 
//判斷Service是否發生死結,如果發生死結,程式將在此一直等待//主要是線程間同步問題 造成死結 
synchronized (mLocks) { } 
} 
以上便是Watchdog監測Service是否正常工作的流程;我們也可以使用Watchdog來監測別的資源如記憶體等使用方式。 
這個Watchdog給我們提供了一種思路,一種架構,對程式正常運行或者資源的正常使用方式等的一種監測機制。