2011-05-09 49 views
0

我正在查看使用報警管理器,並在開發人員文檔中閱讀此內容,但我並不十分了解。報警管理器,使用喚醒鎖確保所有代碼都運行

"If your alarm receiver called Context.startService(), 
it is possible that the phone will sleep before the 
requested service is launched. To prevent this, your 
BroadcastReceiver and Service will need to implement a 
separate wake lock policy to ensure that the phone 
continues running until the service becomes available." 

我特別詢問哪些情況下可能有可能是手機會睡在服務推出之前(因爲這是我不理解的部分)?它取決於手機能夠執行語句的速度有多快?即。它調用startService()打開另一個線程,因此原始線程可以在服務可用之前完成其工作?

感謝

回答

1

如果你開始從一個BroadcastReceiver服務,你只保證設備不會接收機的onReceive時睡覺()。根據this question,startService()是異步的,這意味着在服務啓動時它不會阻止onReceive()完成。所以如果你需要確保服務啓動,你必須實現你自己的WakeLock。