From 927d1af3e0c4f552f9091a4062f1398c7ae776c3 Mon Sep 17 00:00:00 2001 From: kun1h Date: Mon, 5 Feb 2024 10:56:09 +0800 Subject: [PATCH] =?UTF-8?q?1=E5=88=86=E9=92=9F=E4=B8=80=E6=AC=A1gps?= =?UTF-8?q?=EF=BC=8C5=E5=88=86=E9=92=9F=E4=B8=80=E6=AC=A1=E5=9B=BE?= =?UTF-8?q?=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/bing89/travebing/MainActivity.kt | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/bing89/travebing/MainActivity.kt b/app/src/main/java/com/bing89/travebing/MainActivity.kt index ae49ec9..6587cb5 100644 --- a/app/src/main/java/com/bing89/travebing/MainActivity.kt +++ b/app/src/main/java/com/bing89/travebing/MainActivity.kt @@ -60,6 +60,8 @@ class MainActivity : AppCompatActivity() { private var isMonitoring = false private lateinit var lat: String private lateinit var lon: String + private lateinit var latestLat:String + private lateinit var latestLon:String private lateinit var currentImg:String private lateinit var context:Context private lateinit var timer: Timer @@ -91,7 +93,22 @@ class MainActivity : AppCompatActivity() { } getLocation() } + private fun updatePositionCallback():Callback{ + return object:Callback{ + override fun onFailure(call: Call, e: OKIOException) { + Log.d(TAG, "send position to server failed ${e}") + } + override fun onResponse(call: Call, response: Response) { + with(response) { + val s = body?.byteString() + Log.d(TAG, "send position to server succeed ${s}") + body?.close() + } + } + + } + } private fun updateWayCallback(file: File):Callback{ return object:Callback{ override fun onFailure(call: Call, e: OKIOException) { @@ -111,6 +128,25 @@ class MainActivity : AppCompatActivity() { } } + private fun updatePosition(){ + if(lat == latestLat && lon == latestLon){ + return + } + val client = OkHttpClient() + var builder: MultipartBody.Builder = MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("lat", lat) + .addFormDataPart("lon", lon) + val multipartBody: MultipartBody = builder.build() + val url = "http://travebing.bing89.com/api/v1/plans/" + planText.text + "/ways" + Log.d(TAG, "send way to ${url}") + val request = Request.Builder() + .url(url) + .post(multipartBody) + .build() + val call: Call = client.newCall(request) + call.enqueue(updatePositionCallback()) + } private fun updateWay(img:InputStream){ val client = OkHttpClient() var builder: MultipartBody.Builder = MultipartBody.Builder() @@ -139,16 +175,24 @@ class MainActivity : AppCompatActivity() { isMonitoring = true startButton.text = "休息" timer = Timer() + var i = 0 val task = object : TimerTask() { override fun run() { // 每隔 1 秒钟执行一次此方法 // 在这里写下你需要执行的代码 // getLocation() - takePhoto() + i += 1 + + if(i == 5 ){ + takePhoto() + i = 0 + }else{ + updatePosition() + } } } - timer.schedule(task, 0, 300000) // 每隔 300 秒钟执行一次 + timer.schedule(task, 0, 60000) // 每隔 60 秒钟执行一次 // updateWay(img) }