Reputation: 305
I have a problem with an implementation of an interface, I tried some things but without success.
And I don't know what else to try, so I ask for your help, thank you in advance.
My GPSUtils.kt
class GPSUtils(context: Context) {
private var context: Context
private var mSettingsClient: SettingsClient
private var mLocationSettingsRequest: LocationSettingsRequest
private var locationManager: LocationManager
private var locationRequest: LocationRequest
init {
this.context = context
locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
mSettingsClient = LocationServices.getSettingsClient(context)
locationRequest = LocationRequest.create()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = (10 * 1000).toLong()
locationRequest.fastestInterval = (2 * 1000).toLong()
val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
mLocationSettingsRequest = builder.build()
builder.setAlwaysShow(true) //this is the key ingredient
}
fun turnGPSOn(onGpsListener: OnGpsListener) {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (onGpsListener != null) {
onGpsListener!!.gpsStatus(true)
}
} else {
mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(context as Activity) {
if (onGpsListener != null) {
onGpsListener!!.gpsStatus(true)
}
}.addOnFailureListener(context as Activity) { e ->
val statusCode = (e as ApiException).statusCode
when (statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> try {
val rae = e as ResolvableApiException
rae.startResolutionForResult(context as Activity, 1001)
} catch (sie: IntentSender.SendIntentException) { Log.i(TAG, "PendingIntent unable to execute request.") }
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
val errorMessage = "Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings."
Log.e(TAG, errorMessage)
Toast.makeText(context as Activity, errorMessage, Toast.LENGTH_LONG).show()
}
}
}
}
}
interface OnGpsListener {
fun gpsStatus(isGPSEnable: Boolean)
}
}
My AssigmentFragment.kt
class AssigmentFragment : Fragment() {
var isGPS: Boolean = false
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_assigment, container, false)
GPSUtils(activity!!).turnGPSOn { isGPSEnable ->
isGPS = isGPSEnable
}
}
}
And I got this error:
I don't know what else to try, ideas are welcome. Thank you in advance for the help.
Upvotes: 0
Views: 717
Reputation: 459
If you really want to use that short notation., you can write your interface in Java:
interface OnGpsListener {
void gpsStatus(boolean isGPSEnable);
}
This way you can use it in your kotlin code in the following way:
GPSUtils(activity!!).turnGPSOn(OnGpsListener { isGPSEnable ->
isGPS = isGPSEnable
})
The problem is that SAM convertions are available only for java interfaces, this article explains some things about that:
krossovochkin: kotlin java interop function references and sam conversions
And if you are porting java code to kotlin, maybe presenving java interfaces is convenient, as this SO answer says.
Upvotes: 2
Reputation: 247
Try this:
GPSUtils(activity!!).turnGPSOn(object: GPSUtils.OnGpsListener {
override fun gpsStatus(isGPSEnable: Boolean) {
// Code block ...
}
})
Upvotes: 0