简体   繁体   English

Android requestLocationUpdates 不起作用(GPS_PROVIDER、ACCESS_FINE_LOCATION)

[英]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:在 AndroidManifest 我放了这些权限:

<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:这是带有警报的 MainActivity,如果地理位置关闭,则警告用户:

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.那是世袭18.1。 I tried the app inside an emulator and everithing worked correctly.我在模拟器中尝试了该应用程序,一切正常。

It's Lineage 18.1.这是世袭18.1。

Different phones are different phones.不同的手机是不同的手机。 And GPS is highly device specific. GPS 具有高度的设备特定性。

source 资源

A GPS test app has direct access to the hardware. GPS 测试应用程序可以直接访问硬件。 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. Google 地图从 Google Play 服务获取 GPS 坐标,因此,如果您的 Google Play 服务的位置服务未配置为在需要时运行。 it would be a problem.这将是一个问题。

source and possible resolution 来源和可能的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “gps”位置提供程序需要 ACCESS_FINE_LOCATION 权限,适用于 android 6.0 - "gps" location provider requires ACCESS_FINE_LOCATION permission for android 6.0 Android 6.0“gps”位置提供程序需要ACCESS_FINE_LOCATION权限 - Android 6.0 “gps” location provider requires ACCESS_FINE_LOCATION permission 提供者GPS需要ACCESS_FINE_LOCATION权限 - Provider gps requires ACCESS_FINE_LOCATION permission GPS 提供商是否需要 ACCESS_FINE_LOCATION 权限? - Does the GPS provider require the ACCESS_FINE_LOCATION permission or not? 反正有使用Android中的GPS_PROVIDER快速请求LocationUpdates - Is there anyway to requestLocationUpdates fast using GPS_PROVIDER in android Android “gps 需要 ACCESS_FINE_LOCATION” 错误 - Android “gps requires ACCESS_FINE_LOCATION” error GPS_PROVIDER的位置 - Location with GPS_PROVIDER ACCESS_FINE_LOCATION无效 - ACCESS_FINE_LOCATION not working ava.lang.SecurityException:“ gps”位置提供程序需要ACCESS_FINE_LOCATION权限问题 - ava.lang.SecurityException: “gps” location provider requires ACCESS_FINE_LOCATION permission issue 引起:java.lang.SecurityException:“gps”位置提供程序需要ACCESS_FINE_LOCATION权限 - Caused by: java.lang.SecurityException: “gps” location provider requires ACCESS_FINE_LOCATION permission
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM