Skip to content

Instantly share code, notes, and snippets.

@mhd-zulqarnain
Created November 27, 2019 13:25
Show Gist options
  • Save mhd-zulqarnain/ae2ca7619333ecbb0b82f2c60eec3436 to your computer and use it in GitHub Desktop.
Save mhd-zulqarnain/ae2ca7619333ecbb0b82f2c60eec3436 to your computer and use it in GitHub Desktop.
location permission
fun getLocationPermission() {
if (ContextCompat.checkSelfPermission(
this@NearbyRestaurantActivity,
android.Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
mLocationPermissionGranted = true
} /*else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && neverAskAgainSelected(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
Utils.showMsg(this, getString(R.string.location_settings_message))
finish()
}*/ else {
ActivityCompat.requestPermissions(
this,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
for (permission in permissions) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission!!)) { //denied
Log.e("denied", permission)
mLocationPermissionGranted = false
getLocationPermission()
} else {
if (ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) { //allowed
Log.e("allowed", permission)
} else {
displayNeverAskAgainDialog()
// Utils.showMsg(this, getString(R.string.location_settings_message))
}
}
}
}
private fun displayNeverAskAgainDialog() {
val builder= AlertDialog.Builder(this)
builder.setMessage(getString(R.string.location_settings_message)
+ "\n\nSelect Permissions -> Enable Location permission")
builder.setCancelable(false)
builder.setPositiveButton("Permit Manually", DialogInterface.OnClickListener { dialog, which ->
dialog.dismiss()
val intent = Intent()
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
val uri: Uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)
finish()
})
builder.setNegativeButton("Cancel", null)
builder.show()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment