My app contains the following code to check for permission to use the device's location services but I need some way of detecting if Location
in the device is turned on or off and to turn it on if it is off.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_counties)
// selectedCounty = intent.getStringExtra("COUNTY")!!
// Custom action bar code to return to list of counties
// configureCustomActionBar()
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
Log.d("Debug","Permission not granted")
// Permission is not granted
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(this, "Location needed for navigation", Toast.LENGTH_SHORT).show()
} else {
Log.d("Debug","Request Permission")
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), REQUEST_CODE)
}
} else {
// Permission has already been granted
Log.d("Debug", "Permission already granted")
}
}
Although permission is granted I still need some way of turning Location
on or prompting the user to manually turn it on.