获取当前(不是最后)位置,仅在Android中更新一次

时间:2018-04-17 05:09:13

标签: android kotlin

我想在触摸按钮时获取当前位置。

  1. 触摸按钮

  2. requestLocationUpdates 一次,获取当前位置,然后 removeLocationUpdates

  3. 问题在于2.当我 requestLocationUpdates 时,我不希望GPS和wifi一直工作。但我不知道如何 removeLocationUpdates 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 个答案:

答案 0 :(得分:2)

  1. 请求后,您将获得回拨

  2. 您正在呼叫userLocationClient.removeLocationUpdates(userLocationCallback) 在循环中。在获取位置对象

  3. 之后需要在中断后调用

    https://developer.android.com/training/location/receive-location-updates.html#stop-updates

答案 1 :(得分:0)

要在匿名LocationListener对象中使用removeLocationUpdates,您可以执行removeLocationUpdates(this)