Мое приложение не запрашивает у пользователя разрешения на включение Location
. Когда я вручную включаю Location
, мое приложение работает должным образом, но когда я вручную выключаю Location
, оно не работает должным образом и не запрашивает у пользователя разрешения на включение Location
.
Вот мой Kotlin код:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
REQUEST_CODE -> {
if (grantResults.size > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_counties)
selectedCounty = intent.getStringExtra("COUNTY")!!
// Custom action bar code to return to list of counties
val actionBar: ActionBar? = this.supportActionBar
actionBar?.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
actionBar?.setDisplayShowCustomEnabled(true)
actionBar?.setCustomView(R.layout.custom_action_bar_1)
val countiesLabel: TextView = findViewById<TextView>(R.id.countiesTextView)
countiesLabel.text = selectedCounty
val view : View? = actionBar?.customView
actionBar?.setCustomView(view)
sites.clear()
db.collection("UKSites")
.document("England")
.collection("Counties")
.document(selectedCounty!!)
.collection(selectedCounty!!)
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
val site: Site =
Site(document.data["Name"].toString())
site.address.line1 = document.data["Address Line 1"].toString()
site.address.line2 = document.data["Address Line 2"].toString()
site.address.line3 = document.data["Address Line 3"].toString()
site.address.line4 = document.data["Address Line 4"].toString()
site.address.postcode = document.data["Postcode"].toString()
site.address.phoneNumber = document.data["Telephone"].toString()
site.address.siteURL = document.data["Site URL"].toString()
site.description = document.data["Description"].toString()
site.price = document.data["Price"] as Double
site.distance = 0
site.locationCoordinate.Longitude = document.data["Longitude"] as Double
site.locationCoordinate.Latitude = document.data["Latitude"] as Double
sites.add(site)
}
// Check permission
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(
this,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_CODE
)
} else {
BuildLocationRequest()
BuildLocationCallback()
// Create FusedProviderClient
fusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this)
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_CODE
)
} else {
fusedLocationProviderClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.myLooper()
)
}
}
}
.addOnFailureListener { exception ->
Log.w("Error", "Error getting documents: ", exception)
}
}
private fun BuildLocationRequest() {
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 5000
locationRequest.fastestInterval = 3000
locationRequest.smallestDisplacement = 10f
}
private fun BuildLocationCallback() {
locationCallback = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?) {
val currentCoordinate = p0!!.locations.get(p0.locations.size - 1)
// Build URL for web request
var distanceAPIURL : String = BASE_URL + "&origins="
var siteLatitude: Double
var siteLongitude: Double
// add current location parameter to API URL
distanceAPIURL += "${currentCoordinate.latitude},${currentCoordinate.longitude}"
// add site destinations to API URL
distanceAPIURL += "&destinations="
// Build API request from site locations
for (site in sites) {
siteLatitude = site.locationCoordinate.Latitude!!
siteLongitude = site.locationCoordinate.Longitude!!
if (site == sites.first()) {
distanceAPIURL += "${siteLatitude}%2C${siteLongitude}"
} else {
distanceAPIURL += "%7C${siteLatitude}%2C${siteLongitude}"
}
}
// Add API KEY
distanceAPIURL += GOOGLE_API_KEY
// Make web requests to Google
val distanceRequest = object : StringRequest(Method.POST, distanceAPIURL, Response.Listener { response ->
// Parse out distances from returned data
siteDistances = parseOutDistanceValues(response)
// Update distance information for each site
for (siteIndex in 0 until sites.size) {
sites[siteIndex].distance = siteDistances[siteIndex]
}
// Sort sites in ascending order of distance
sites = sortByDistance(sites)
// Recycler View code here
recyclerView = findViewById<View>(R.id.recyclerView) as RecyclerView
adapter = SiteAdapter(this@CountiesActivity, sites, selectedCounty!!)
val layoutManager = LinearLayoutManager(applicationContext)
recyclerView!!.layoutManager = layoutManager
recyclerView!!.itemAnimator = DefaultItemAnimator()
// Add a neat dividing line between items in the list
recyclerView!!.addItemDecoration(DividerItemDecoration(this@CountiesActivity, LinearLayoutManager.VERTICAL))
recyclerView!!.addItemDecoration(
ItemOffsetDecoration(
applicationContext,
0,
0
)
)
// set the adapter
recyclerView!!.adapter = adapter
}, Response.ErrorListener { error ->
Log.d("ERROR", "Could not calculate distances to sites: ${error.localizedMessage}")
}) {
}
Volley.newRequestQueue(applicationContext).add(distanceRequest)
}
}
}