Created
April 26, 2018 06:15
-
-
Save sudhanshu-15/f361c725868e018f64901d483e4231b1 to your computer and use it in GitHub Desktop.
Sample code to handle permissions in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Request App Permissions | |
private void permissionsRequest(){ | |
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) | |
!= PackageManager.PERMISSION_GRANTED | |
|| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | |
!= PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION}, 111); | |
}else{ | |
startARActivity(); | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
switch (requestCode) { | |
case 111: | |
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ | |
startARActivity(); | |
}else{ | |
permissionsNotGranted(); | |
} | |
} | |
} | |
//Function to handle when Permissions are not granted | |
private void permissionsNotGranted(){ | |
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); | |
alertBuilder.setTitle(getString(R.string.no_permission_title)); | |
alertBuilder.setMessage(getString(R.string.no_permission_body)); | |
alertBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
dialogInterface.cancel(); | |
System.exit(1); | |
} | |
}); | |
AlertDialog noPermission = alertBuilder.create(); | |
noPermission.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment