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) }