简体   繁体   中英

Android requestLocationUpdates not working (GPS_PROVIDER, ACCESS_FINE_LOCATION)

I'm trying to implement geolocation feature on my app, but I miss something.

Could you guys help me?

Inside AndroidManifest I put these permission:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

This is the MainActivity with an alert to warn the user if the geolocation is off:

class MainActivity : AppCompatActivity() {

    private lateinit var mLocationManager : LocationManager
    val mLocationListener = LocationListener {
        Log.i("Location", "latitude ${it.latitude}, logitude ${it.longitude}")
    }

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

        mLocationManager = getSystemService(LOCATION_SERVICE) as LocationManager

        if (ActivityCompat.checkSelfPermission(
            this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,),
                7777
            )
        }
        checkGPSEnabled()
    }

    @SuppressLint("LongLogTag", "MissingPermission")
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        Log.i("onRequestPermissionsResult", "requestCode $requestCode, grantResults $grantResults")
        permissions.forEach {
            Log.i("onRequestPermissionsResult", "permission $it")
        }

        when(requestCode) {
            7777 -> {
                Log.i("onRequestPermissionsResult", "request sended")
                mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    1000,
                    50F,
                    mLocationListener
                )
            }
        }
    }

    private fun checkGPSEnabled() {
        if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            val alertDialog: AlertDialog = AlertDialog.Builder(this).create()
            alertDialog.setMessage("Activate geolocation!")
            alertDialog.setButton(
                AlertDialog.BUTTON_NEUTRAL,
                "OK"
            ) { dialog, _ -> dialog.dismiss() }
            alertDialog.show()
        }
    }
}

UPDATE

It was Lineage 18.1. I tried the app inside an emulator and everithing worked correctly.

It's Lineage 18.1.

Different phones are different phones. And GPS is highly device specific.

source

A GPS test app has direct access to the hardware. However, this is NOT how Google Maps works. Google Maps gets the GPS coordinates from Google Play Services, Thus, if the location service of your Google Play Services is not configured to operate whenever needed. it would be a problem.

source and possible resolution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM