Home >Java >javaTutorial >Why Am I Getting a 'PERMISSION_DENIED' Error When Requesting Location Permission on Android?

Why Am I Getting a 'PERMISSION_DENIED' Error When Requesting Location Permission on Android?

Susan Sarandon
Susan SarandonOriginal
2024-12-11 11:14:15803browse

Why Am I Getting a 'PERMISSION_DENIED' Error When Requesting Location Permission on Android?

How to Request Location Permission at Runtime

Problem:

When adding Location permissions to the manifest and running on Android 6, no response or location updates are received.

Question:

What am I doing wrong? (Error code in the stack trace is 'PERMISSION_DENIED')

Answer:

To request Location permission at runtime, you need to explicitly ask for it using the ActivityCompat.requestPermissions() method.

Updated Code with Kotlin and Example for Android 12:

Kotlin:

import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var locationManager: LocationManager
    private var provider: String? = null
    private val locationListener = MyLocationListener()

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

        locationManager = getSystemService(LocationManager::class.java)
        provider = locationManager.getBestProvider(null, false)

        if (checkPermission()) {
            getLocationUpdates()
        } else {
            requestPermission()
        }
    }

    override fun onResume() {
        super.onResume()
        if (checkPermission()) {
            getLocationUpdates()
        }
    }

    override fun onPause() {
        super.onPause()
        if (checkPermission()) {
            locationManager.removeUpdates(locationListener)
        }
    }

    private fun getLocationUpdates() {
        locationManager.requestLocationUpdates(provider, 0, 0f, locationListener)
        val location = locationManager.getLastKnownLocation(provider)
        if (location != null) {
            onLocationChanged(location)
        }
    }

    private fun onLocationChanged(location: Location) {
        val latitude = location.latitude
        val longitude = location.longitude
        Toast.makeText(this, "Location: Lat: $latitude, Lng: $longitude", Toast.LENGTH_LONG).show()
    }

    private fun checkPermission(): Boolean {
        return ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
    }

    private fun requestPermission() {
        ActivityCompat.requestPermissions(this, arrayOf(ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getLocationUpdates()
            } else {
                // Permission denied.
                AlertDialog.Builder(this)

The above is the detailed content of Why Am I Getting a 'PERMISSION_DENIED' Error When Requesting Location Permission on Android?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn