Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean checkLocationPermission() {
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- // Should we show an explanation?
- if (ActivityCompat.shouldShowRequestPermissionRationale(this,
- Manifest.permission.ACCESS_FINE_LOCATION)) {
- // Show an explanation to the user *asynchronously* -- don't block
- // this thread waiting for the user's response! After the user
- // sees the explanation, try again to request the permission.
- new AlertDialog.Builder(this)
- .setTitle(R.string.title_location_permission)
- .setMessage(R.string.text_location_permission)
- .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- //Prompt the user once explanation has been shown
- ActivityCompat.requestPermissions(SalesPersonActivity.this,
- new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
- MY_PERMISSIONS_REQUEST_LOCATION);
- }
- })
- .create()
- .show();
- } else {
- // No explanation needed, we can request the permission.
- ActivityCompat.requestPermissions(this,
- new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
- MY_PERMISSIONS_REQUEST_LOCATION);
- }
- return false;
- } else {
- return true;
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode,
- String permissions[], int[] grantResults) {
- switch (requestCode) {
- case MY_PERMISSIONS_REQUEST_LOCATION: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // permission was granted, yay! Do the
- // location-related task you need to do.
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.ACCESS_FINE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- //Request location updates:
- getLocation();
- }
- } else {
- // permission denied, boo! Disable the
- // functionality that depends on this permission.
- checkLocationPermission();
- }
- return;
- }
- }
- String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
- getBulkPermissions(permissions, new RequestPermissionAction() {
- @Override
- public void permissionDenied() {
- // TODO: 5/27/2018 handle permission deny
- }
- @Override
- public void permissionGranted() {
- // TODO: 5/27/2018 you code do further operations
- }
- });
- RequestPermissionAction onPermissionCallBack;
- private final static int REQUEST_BULK_PERMISSION = 3006;
- private boolean checkBulkPermissions(String[] permissions) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- for (String permission : permissions) {
- if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
- return false;
- }
- }
- return true;
- } else {
- return true;
- }
- }
- public void getBulkPermissions(String[] permissions, RequestPermissionAction onPermissionCallBack) {
- this.onPermissionCallBack = onPermissionCallBack;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (!checkBulkPermissions(permissions)) {
- requestPermissions(permissions, REQUEST_BULK_PERMISSION);
- return;
- }
- }
- if (onPermissionCallBack != null)
- onPermissionCallBack.permissionGranted();
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- if (onPermissionCallBack != null)
- onPermissionCallBack.permissionGranted();
- } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
- if (onPermissionCallBack != null)
- onPermissionCallBack.permissionDenied();
- }
- }
- public interface RequestPermissionAction {
- void permissionDenied();
- void permissionGranted();
- }
Add Comment
Please, Sign In to add comment