Skip to content

Instantly share code, notes, and snippets.

@wajahatkarim3
Created May 1, 2020 23:57
Show Gist options
  • Save wajahatkarim3/779bf59867696f5574e7e331ca3ed8b8 to your computer and use it in GitHub Desktop.
Save wajahatkarim3/779bf59867696f5574e7e331ca3ed8b8 to your computer and use it in GitHub Desktop.
// Requesting Location Permission
bi.btnRequestPermission.setOnClickListener {
askLocationPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
}
// Single Permission Contract
private val askLocationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { result ->
if(result){
Log.e("TAG", "Location permnission granted")
}else{
Log.e("TAG", "Location permnission denied")
}
}
// -------------------------------------------------------------------
// Requesting Mutliple Permissions - Location & Bluetooth
bi.btnRequestPermission.setOnClickListener {
askMultiplePermissions(arrayOf(
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.BLUETOOTH
))
}
// Mutliple Permissions Contract
private val askMultiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {map ->
for (entry in map.entries)
{
Toast.makeText(this, "${entry.key} = ${entry.value}", Toast.LENGTH_SHORT).show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment