【发布时间】:2020-03-12 16:47:27
【问题描述】:
我在实现接口时遇到问题,我尝试了一些方法但没有成功。
我不知道还能尝试什么,所以我请求你的帮助,提前谢谢你。
我的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)
}
}
我的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
}
}
}
我得到了这个错误:
我不知道还有什么可以尝试的,欢迎提出想法。 提前感谢您的帮助。
【问题讨论】:
标签: android android-studio kotlin