Get current(not last) location with update only one time in Android

I want to get current location when I touch a button.

  1. Touch a button

  2. requestLocationUpdates one time, and get current location, then removeLocationUpdates.

The problem is in 2. I don't want GPS and wifi work all time when I requestLocationUpdates. But I don't know how to removeLocationUpdates in locationCallback.

package com.example.location

import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.util.Log
import android.widget.Button
import android.widget.TextView
import com.google.android.gms.location.*

class MainActivity : AppCompatActivity() {


private var userAgreePermissionCode = 1


private lateinit var userLocationClient: FusedLocationProviderClient


private lateinit var userLocationCallback: LocationCallback

@SuppressLint("RestrictedApi")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    val somePermission = arrayOf(android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION)
    for (p in somePermission ){
        if(ActivityCompat.checkSelfPermission(this, p) != PackageManager.PERMISSION_GRANTED)
            ActivityCompat.requestPermissions(this, somePermission , userAgreePermissionCode)
        break
    }


    findViewById<Button>(R.id.Locate).setOnClickListener{


        userLocationClient = LocationServices.getFusedLocationProviderClient(this)


        val userLocationRequest = LocationRequest().apply {
            interval = 1000  
            fastestInterval = 1000
            priority = LocationRequest.PRIORITY_HIGH_ACCURACY
        }

        //  Problem is here
        userLocationCallback = object : LocationCallback() {
            override fun onLocationResult(locationResult: LocationResult?) {
                locationResult ?: return
                for (location in locationResult.locations){

                    //  I want to get the first update location(current location) and close requestLocationUpdates!!!!
                    //  Only one time, not keep update
                    Log.i("longitude", location.longitude.toString())
                    Log.i("latitude", location.latitude.toString())
                    //  findViewById<TextView>(R.id.Longitude).text = location.longitude.toString()
                    //  findViewById<TextView>(R.id.Latitude).text = location.latitude.toString()

                    //  Problem here, it's error.
                    userLocationClient.removeLocationUpdates(userLocationCallback)
                }
            }
        }


        userLocationClient.requestLocationUpdates(userLocationRequest,userLocationCallback,null)

    }
}

//  -----------------------------------------
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
    when (requestCode) {
        userAgreePermissionCode -> {
            for( i in 0..(grantResults.size-1) ){
                if ((grantResults.isNotEmpty() && grantResults[i] == PackageManager.PERMISSION_GRANTED))
                    Log.i("Status:", "Agree a permission")
                else
                    finish()
            }
            return
        }
    }
}
//  -----------------------------------------

}

2 answers