Android Service相關知識

2021-07-11 13:43:16 字數 831 閱讀 9280

public

void

oncreate()

} l.d(wmodel.time, "oncreate耗時" + (system.currenttimemillis() - s1));

}public

static string getprocessname(context cxt, int pid)

if (procinfo.pid == pid)

} return

null;

}

start_sticky:如果service程序被kill掉,保留service的狀態為開始狀態,但不保留遞送的intent物件。隨後系統會嘗試重新建立service,由於服務狀態為開始狀態,所以建立服務後一定會呼叫onstartcommand(intent,int,int)方法。如果在此期間沒有任何啟動命令被傳遞到service,那麼引數intent將為null。

start_not_sticky:「非粘性的」。使用這個返回值時,如果在執行完onstartcommand後,服務被異常kill掉,系統將會把它置為started狀態,系統不會自動重啟該服務,直到startservice(intent intent)方法再次被呼叫。

start_redeliver_intent:重傳intent。使用這個返回值時,如果在執行完onstartcommand後,服務被異常kill掉,系統會自動重啟該服務,並將intent的值傳入。

start_sticky_compatibility:start_sticky的相容版本,但不保證服務被kill後一定能重啟。

使用startforeground()

Android Service簡單總結

分兩種 local service 不少人又稱之為 本地服務 是指client service同處於乙個程序 remote service 又稱之為 遠端服務 一般是指service處於單獨的乙個程序中 remote service 常用的有兩種實現 messenger 信使 使用廣播通訊,serv...

Android Service 啟動服務

你能夠通過把乙個intent物件 指定要啟動的服務 傳遞給startservice 方法,從乙個activity或其他的應用程式元件啟動服務。android系統呼叫服務的onstartcommand 方法,並且給它傳遞intent物件 你不應該直接呼叫onstartcommand 方法 例如,乙個a...

Android Service 終止服務

被啟動型別的服務必須管理它自己的生命週期。也就是說除非系統要 系統記憶體,否則系統不會終止或銷毀這個服務,在onstartcommand 方法返回後,這個服務會繼續執行。因此而這種型別的服務必須通過呼叫stopself 方法或另乙個元件通過呼叫stopservice 方法才能終止。一旦用stopse...