Untuk kotlin
private fun isLocationEnabled(mContext: Context): Boolean {
val lm = mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(
LocationManager.NETWORK_PROVIDER)
}
dialog
private fun showLocationIsDisabledAlert() {
alert("We can't show your position because you generally disabled the location service for your device.") {
yesButton {
}
neutralPressed("Settings") {
startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
}
}.show()
}
sebut seperti ini
if (!isLocationEnabled(this.context)) {
showLocationIsDisabledAlert()
}
Petunjuk: dialog membutuhkan impor berikut (studio android harus menangani ini untuk Anda)
import org.jetbrains.anko.alert
import org.jetbrains.anko.noButton
Dan dalam manifes Anda memerlukan izin berikut
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>