package com.casic.dcms.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;

import androidx.annotation.Nullable;

import com.amap.api.location.AMapLocation;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.LocationHelper;
import com.casic.dcms.utils.NotificationHelper;
import com.casic.dcms.utils.callback.ILocationListener;
import com.casic.dcms.view.MainActivity;

public class UpdateLocationService extends Service {

    private static final String TAG = "UploadLocationService";

    @Override
    public void onCreate() {
        super.onCreate();
        NotificationHelper notificationHelper = NotificationHelper.get(this);
        Notification notification = notificationHelper.createKeepAlive();
        startForeground(Integer.MAX_VALUE, notification);
        LocationHelper.obtainCurrentLocation(this, new ILocationListener() {

            @Override
            public void onAMapLocationGet(AMapLocation aMapLocation) {
                Message message = MainActivity.weakReferenceHandler.obtainMessage();
                message.what = LocaleConstant.UPDATE_LOCATION;
                message.obj = aMapLocation;
                MainActivity.weakReferenceHandler.sendMessage(message);
            }
        }, false);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 如果Service被终止，当资源允许情况下，重启service
        return START_STICKY;
    }

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