Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet152/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   148   149   150   151   152   153   154   155   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

177
CHAPTER 8: APIs
fusedLocationClient?.lastLocation?.
addOnSuccessListener(this,
{location : Location? ->
// Got last known location. In some rare
// situations this can be null.
if(location == null) {
// TODO, handle it
} else location.apply {
// Handle location object
Log.e("LOG", location.toString())
}
})
}
Here, checkPermission() checks and possibly acquires the needed permissions as 
described in Chapter 
7
. This could be, for example, the following:
val PERMISSION_ID = 42
private fun checkPermission(vararg perm:String) :
Boolean {
val havePermissions = perm.toList().all {
ContextCompat.checkSelfPermission(this,it) ==
PackageManager.PERMISSION_GRANTED
}
if (!havePermissions) {
if(perm.toList().any {
ActivityCompat.
shouldShowRequestPermissionRationale(this, it)}
) {
val dialog = AlertDialog.Builder(this)
.setTitle("Permission")
.setMessage("Permission needed!")
.setPositiveButton("OK",{
id, v ->
ActivityCompat.requestPermissions(
this, perm, PERMISSION_ID)
})
.setNegativeButton("No",{
id, v ->
})
.create()
dialog.show()
} else {
ActivityCompat.requestPermissions(this, perm,
PERMISSION_ID)
}
return false
}
return true
}


178
CHAPTER 8: APIs
For simplicity I used strings for button labels and messages. For production code, make sure 
you use resources! The function checkPermission(), if necessary, tries to acquire permission 
from a system activity. Whether the user grants permissions, upon return from this activity, 
your app may accordingly react to the result.
override
fun onRequestPermissionsResult(requestCode: Int,
permissions: Array,
grantResults: IntArray) {
when (requestCode) {
PERMISSION_ID -> {
...
}
...
}
}
Tracking Position Updates
If your app needs to track updates on changing locations, you follow a different approach. 
First, the permissions needed are the same as stated earlier for the last known location, 
so there’s no change there. The difference is in requesting periodic updates from the 
fused location provider. For that we need to define a location settings object. Confusingly, 
the corresponding class is called LocationRequest (it would have been better as 
LocationRequestSettings or something else). To create one, write the following:
val reqSetting = LocationRequest.create().apply {
fastestInterval = 10000
interval = 10000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
smallestDisplacement = 1.0f
}
The .apply construct lets us configure the object faster. For example, fastestInterval = 
10000 internally gets translated to reqSetting.setFastestInterval(10000). The meanings of 
the individual settings are as follows:

fastestInterval
The fastest possible total update interval in milliseconds for the location 
provider.

interval
Caution The concept of a “last known location” is somewhat blurry. In an emulated device, for 
example, it is not sufficient to set the location by the provided device control for changing the last 
known location. Only after an app like Google Maps uses location update mechanisms does the 
code described here return the correct value. The mechanisms described in the following sections 
are more complex but also more reliable.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   148   149   150   151   152   153   154   155   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling