package com.tofly.ghrq_route.module.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.Nullable;
import android.util.Log;

import com.tofly.ghrq_route.module.OfflineWork.OfflineWorkTool;
import com.tofly.ghrq_route.common.util.AlarmUtils.AlarmUtils;

/**
 * Created by Amu on 2017/4/14.
 */

public class OfflineWorkerService extends Service {

    private static final String TAG = OfflineWorkerService.class.getName();

    private static boolean isRunning = false;

    private OfflineWorkTool offlineWorkTool;

    private static final int WAKE_TIME = 1 * 60 * 1000; //唤醒时间间隔(ms)

    public static final int REQCODE_OFFLINE_WORK = 300;

    public static final String ACTION_OFFLINE_WORK = "com.route.OfflineWorkerService.wakeup";

    @Nullable @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        offlineWorkTool = OfflineWorkTool.getInstance();
        offlineWorkTool.startChecking(this);
        AlarmUtils.getInstance().startBroadcast(WAKE_TIME, AlarmReceiver.class, ACTION_OFFLINE_WORK, REQCODE_OFFLINE_WORK);

        Log.d(TAG, "onStartCommand");
        flags = START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        offlineWorkTool.stopChecking(this);
        AlarmUtils.getInstance().stopBroadcast(AlarmReceiver.class, ACTION_OFFLINE_WORK, REQCODE_OFFLINE_WORK);
        super.onDestroy();
    }
}
