FusedLocationProviderClient を使用する場合は、まず、デバイスで Location が有効になっていることを確認する必要があります。
private fun isLocationEnabled(): Boolean {
val activity: Activity? = this.activity
val lm = activity?.getSystemService(Context.LOCATION_SERVICE) as? LocationManager
if (lm != null) {
val enabled = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
lm.isLocationEnabled
} else {
lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
|| lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
}
Log.d(TAG, "isLocationServiceEnabled", enabled)
return enabled
}
return false
}
次に、LocationSettings を確認します。
val request = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
val settingRequest = LocationSettingsRequest.Builder()
.addLocationRequest(request)
.build()
// check Location settings for the request
LocationServices.getSettingsClient(context)
.checkLocationSettings(settingRequest)
.addOnSuccessListener {
// OK!!
// fusedLocationProviderClient.requestLocationUpdates
}
.addOnFailureListener { exception ->
if (exception is ResolvableApiException) {
try {
exception.startResolutionForResult(activity, RC_LOCATION_SETTINGS)
} catch (ex: IntentSender.SendIntentException) {
// ignore
}
} else {
// handle other errors
showFusedDisabledDialog()
}
}
また、以下のリンクもご確認ください。
https://developer.android.com/training/location/change-location-settings