a、extends IntentService,重寫onHandleIntent方法,在裏面加入你自己的業務邏輯
b、startService(傳入Intent對象)
0、一個參數,接受一個Intent對象
protected abstract void onHandleIntent(@Nullable Intent intent);
根據不同的業務,傳入不同狀態的Intent對象,這樣在該方法內部就能區分要進行的不同耗時工作了
1、三個參數,接受一個Intent對象、一個int、一個int
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
startService調用後,onStartCommand方法就會被回調
第一個Intent對象:在startService中傳入的Intent對象
第二個flags則代表:Additional data about this start request.
第三個startId則代表:A unique integer representing this specific request to start. Use with {@link #stopSelfResult(int)}.
可見startId則是stopSelfResult方法就用到的,官方也說明你去看stopSelf方法就對了