Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet156/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   152   153   154   155   156   157   158   159   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

182
CHAPTER 8: APIs
", Longitude = " +
location.getLongitude(), e)
}
if (addresses == null || addresses.size == 0) {
// No address was found.
if (errorMessage.isEmpty()) {
errorMessage = "no_address_found"
Log.e("LOG", errorMessage)
}
deliverResultToReceiver(
receiver,
GeocoderConstants.FAILURE_RESULT,
errorMessage)
} else {
val address = addresses[0]
val addressFragments =
(0..address.maxAddressLineIndex).
map { i -> address.getAddressLine(i) }
val addressStr = addressFragments.joinToString(
separator =
System.getProperty("line.separator"))
Log.i("LOG", "address_found")
deliverResultToReceiver(
receiver,
GeocoderConstants.SUCCESS_RESULT,
addressStr)
}
}
private fun deliverResultToReceiver(
receiver:ResultReceiver,
resultCode: Int,
message: String) {
val bundle = Bundle()
bundle.putString(GeocoderConstants.RESULT_DATA_KEY,
message)
receiver.send(resultCode, bundle)
}
}
Again, for productive code, you should use resource strings instead of literals, as shown in 
this example. The service must be registered inside AndroidManifest.xml.
android:exported="false"/>
For using this service, we first build a ResultReceiver class and check the permissions.
For example, we use the last known location to call the service.
class AddressResultReceiver(handler: Handler?) :
ResultReceiver(handler) {
override


183
CHAPTER 8: APIs
fun onReceiveResult(resultCode: Int,
resultData: Bundle) {
val addressOutput =
resultData.getString(
GeocoderConstants.RESULT_DATA_KEY)
Log.e("LOG", "address result = " +
addressOutput.toString())
...
}
}
val resultReceiver = AddressResultReceiver(null)
fun startFetchAddress(view:View) {
if (checkPermission(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION))
{
fusedLocationClient?.lastLocation?.
addOnSuccessListener(this, {
location: Location? ->
if (location == null) {
// TODO
} else location.apply {
Log.e("LOG", toString())
val intent = Intent(
this@MainActivity,
FetchAddressService::class.java)
intent.putExtra(
GeocoderConstants.RECEIVER,
resultReceiver)
intent.putExtra(
GeocoderConstants.LOCATION_DATA_EXTRA,
this)
startService(intent)
}
})
}
}
You can see we use an explicit intent; that is why we don’t need an intent filter inside the 
service declaration in AndroidManifest.xml.
Using ADB to Fetch Location Information
For development and debugging purposes, you can use ADB to fetch the location 
information of a device connected to your PC or laptop.
./adb shell dumpsys location
For more information on CLI commands, see Chapter 
18
.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   152   153   154   155   156   157   158   159   ...   223




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