package com.dahe.gldriver.service import android.app.Service import android.content.Intent import android.os.Environment import android.os.Handler import android.os.IBinder import android.os.Message import android.util.Log import com.amap.api.location.AMapLocation import com.dahe.gldriver.bean.OrderDetailBean import com.dahe.gldriver.bean.UpLocation import com.dahe.gldriver.net.BaseObserver import com.dahe.gldriver.net.DataManager import com.dahe.gldriver.net.RxHttpCallBack import com.dahe.gldriver.utils.GDLocationUtils import com.dahe.mylibrary.net.CommonResponseBean import com.dahe.mylibrary.utils.CrashHandler import com.dahe.mylibrary.utils.CrashHandler2 import com.dahe.mylibrary.utils.FileIOUtils import com.dahe.mylibrary.utils.ToastUtils import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.schedulers.Schedulers import java.io.File import java.io.FileOutputStream import java.io.PrintWriter import java.io.StringWriter import java.io.Writer import java.text.DateFormat import java.text.SimpleDateFormat import java.util.Date import java.util.Timer import java.util.TimerTask /** * @ClassName UpLocationService * @Author john * @Date 2024/3/28 09:08 * @Description TODO */ class UpLocationService : Service() { private val hd = Handler { msg: Message -> when (msg.what) { 123 -> { //获取文件输出路径 val path = Environment.getExternalStorageDirectory() .toString() + "/crashinfo/" val time: String = formatter.format(Date()) val time2: String = formatter2.format(Date()) val fileName = "crash-$time.txt" GDLocationUtils.instance.getLocation(application) { if (it.getErrorCode() == 0 && it.latitude != 0.0) { FileIOUtils.writeFileFromString( path + fileName, """当前时间:${time2},经纬度:${it.latitude}:${it.longitude}""", true ) } else { FileIOUtils.writeFileFromString( path + fileName, """当前时间:${time2},经纬度:${it.latitude}${it.errorInfo}""", true ) } } // var orderString = BaseSPUtils.get(application,AppConfig.NEED_UP_ORDER,"") as String // if (!orderString.isNullOrEmpty()){ // var order = Gson().fromJson(orderString,OrderDetailBean::class.java) // GDLocationUtils.instance.getLocation(application){ // loadLocation(it,order) // // } // }else{ // // } } } false } private val formatter: DateFormat = SimpleDateFormat("yyyy-MM-dd") private val formatter2: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") override fun onBind(intent: Intent?): IBinder? { return null } override fun onRebind(intent: Intent?) { super.onRebind(intent) } private val timeSum = 1000 * 60 * 3 // private val timeSum = 3000 private var timer: Timer? = null override fun onCreate() { super.onCreate() Log.e("开始循环上传,等待100ms", "-----") timer = Timer() timer?.schedule(object : TimerTask() { override fun run() { val msg = Message.obtain() msg.what = 123 hd.sendMessage(msg) } }, 1000, timeSum.toLong()) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { return START_STICKY } private fun loadLocation(loc: AMapLocation, order: OrderDetailBean) { DataManager.getInstance().driverUploadLocus( UpLocation( loc.latitude, loc.longitude, order.orderId, order.waybillId ) ) .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) .subscribe(BaseObserver(application, object : RxHttpCallBack() { override fun onSuccess(t: CommonResponseBean) { super.onSuccess(t) } })) } override fun onDestroy() { super.onDestroy() if (timer != null) { timer?.cancel() timer = null } } }