Java Code Examples for android.support.v4.app.ActivityCompat.shouldShowRequestPermissionRationale()

The following are Jave code examples for showing how to use shouldShowRequestPermissionRationale() of the android.support.v4.app.ActivityCompat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Sega   File: ImagePickerActivity.java   View Source Code Vote up 7 votes
private void requestCameraPermission() {
    Log.w(TAG, "Write External permission is not granted. Requesting permission");

    final String[] permissions = new String[]{Manifest.permission.CAMERA};

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
        ActivityCompat.requestPermissions(this, permissions, Constants.PERMISSION_REQUEST_CAMERA);
    } else {
        if (!isPermissionRequested(Constants.PREF_CAMERA_REQUESTED)) {
            ActivityCompat.requestPermissions(this, permissions, Constants.PERMISSION_REQUEST_CAMERA);
            setPermissionRequested(Constants.PREF_CAMERA_REQUESTED);
        } else {
            Snackbar snackbar = Snackbar.make(mainLayout, R.string.msg_no_camera_permission,
                    Snackbar.LENGTH_INDEFINITE);
            snackbar.setAction(R.string.ok, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    openAppSettings();
                }
            });
            snackbar.show();
        }
    }
}
 
Example 2
Project: HackJakRawan   File: MapsActivity.java   View Source Code Vote up 7 votes
public boolean checkLocationPermission(){
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                android.Manifest.permission.ACCESS_FINE_LOCATION)) {
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}
 
Example 3
Project: PermissionsWrapper   File: RequestPermissionsActv.java   View Source Code Vote up 6 votes
private void checkPermission() {
    int deniedIndex = checkSelfPermissions(permissions);
    if (deniedIndex != -1) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[deniedIndex])) {
            if (TextUtils.isEmpty(rationalMessage)) {
                requestPermission();
            } else {
                showRationalDialog();
            }

        } else {
            requestPermission();
        }
    } else {
        sendGrantMessage();
    }
}
 
Example 4
Project: GogoNew   File: MapsActivity.java   View Source Code Vote up 6 votes
private void requestPermissions() {
        boolean shouldProvideRationale =
                ActivityCompat.shouldShowRequestPermissionRationale(this,
                        android.Manifest.permission.ACCESS_COARSE_LOCATION);

        // Provide an additional rationale to the user. This would happen if the user denied the
        // request previously, but didn't check the "Don't ask again" checkbox.
        if (shouldProvideRationale) {

                Log.i(TAG, "Displaying permission rationale to provide additional context.");

        } else {
                Log.i(TAG, "Requesting permission");
                // Request permission. It's possible this can be auto answered if device policy
                // sets the permission in a given state or the user denied the permission
                // previously and checked "Never ask again".
                startLocationPermissionRequest();
        }
}
 
Example 5
Project: CXJPadProject   File: EasyPermissions.java   View Source Code Vote up 6 votes
private static boolean shouldShowRequestPermissionRationale(@NonNull Object object,
                                                            @NonNull String perm) {
    if (object instanceof Activity) {
        return ActivityCompat.shouldShowRequestPermissionRationale((Activity) object, perm);
    } else if (object instanceof Fragment) {
        return ((Fragment) object).shouldShowRequestPermissionRationale(perm);
    } else if (object instanceof android.app.Fragment) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return ((android.app.Fragment) object).shouldShowRequestPermissionRationale(perm);
        } else {
            throw new IllegalArgumentException(
                    "Target SDK needs to be greater than 23 if caller is android.app.Fragment");
        }
    } else {
        throw new IllegalArgumentException("Object was neither an Activity nor a Fragment.");
    }
}
 
Example 6
Project: haven   File: CustomSlideNotify.java   View Source Code Vote up 6 votes
private void askForPermission(String permission, Integer requestCode) {
    if (ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permission)) {

            //This is called if user has denied the permission before
            //In this case I am just asking the permission again
            ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode);

        } else {

            ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode);
        }
    } else {
    }
}
 
Example 7
Project: mapbox-events-android   File: PermissionsManager.java   View Source Code Vote up 5 votes
private void requestPermissions(Activity activity, String[] permissions) {
  ArrayList<String> permissionsToExplain = new ArrayList<>();
  for (String permission : permissions) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
      permissionsToExplain.add(permission);
    }
  }

  if (listener != null && permissionsToExplain.size() > 0) {
    // The developer should show an explanation to the user asynchronously
    listener.onExplanationNeeded(permissionsToExplain);
  }

  ActivityCompat.requestPermissions(activity, permissions, REQUEST_PERMISSIONS_CODE);
}
 
Example 8
Project: HeadlineNews   File: PermissionUtils.java   View Source Code Vote up 5 votes
/**
 * 是否有权限需要说明提示
 */
private static boolean shouldShowRequestPermissionRationale(final Context context, final String... deniedPermissions) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false;
    boolean rationale;
    for (String permission : deniedPermissions) {
        rationale = ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permission);
        if (rationale) return true;
    }
    return false;
}
 
Example 9
Project: Ocr-android   File: ChoosePicActivity.java   View Source Code Vote up 5 votes
private void requestStoragePermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        new PermissionDeclarationDialogFragment().show(getSupportFragmentManager(), FRAGMENT_DIALOG);
    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION);
    }
}
 
Example 10
Project: godlibrary   File: PermissionUtil.java   View Source Code Vote up 5 votes
/**
 * 用户拒绝的权限
 */
private static boolean shouldShowRequestPermissionRationale(final Activity activity, String[] permissions) {
    StringBuilder msgbff = new StringBuilder();
    if (permissions.length < 1) {
        return false;
    }
    // Verify that each required permission has been granted, otherwise return false.
    for (String p : permissions) {
        //如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
        //如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
        //如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, p)) {
            msgbff.append(getPermissionCN(p)).append("\n");
        }
    }
    String msg = msgbff.toString();
    if (msg.length() > 0) {
        final DialogMsgBox dialogMsgBox = new DialogMsgBox(activity);
        msg ="请授予以下权限:\n"+msg;
        dialogMsgBox.show("权限请求", msg);
        dialogMsgBox.but_ok.setText("去设置");
        dialogMsgBox.but_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogMsgBox.dismiss();
                AndroidUtils.showInstalledAppDetails(activity, activity.getPackageName());
            }
        });
        return true;
    }
    return false;
}
 
Example 11
Project: FlickLauncher   File: RemoteShortcuts.java   View Source Code Vote up 5 votes
/**
 * Make request permission
 * @param activity Activity
 */
private static void requestPermission(Activity activity) {

    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        Log.d(TAG, "Write External Storage permission allows us to do store shortcuts data. Please allow this permission in App Settings.");
    } else {
        ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 111);
        Log.d(TAG, "Write External Storage permission allows us to do store shortcuts data.");
    }
}
 
Example 12
Project: lrs_android   File: PermissionManager.java   View Source Code Vote up 5 votes
/**
 * 获取权限集中需要申请权限的列表
 *
 * @param permissions
 * @return
 * @since 2.5.0
 */
private static List<String> findDeniedPermissions(String[] permissions) {
    List<String> needRequestPermissonList = new ArrayList<String>();
    for (String perm : permissions) {
        if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED) {
            needRequestPermissonList.add(perm);
        } else if (ActivityCompat.shouldShowRequestPermissionRationale(context, perm)) {
            needRequestPermissonList.add(perm);
        }
    }
    return needRequestPermissonList;
}
 
Example 13
Project: ankihelper   File: CustomDictionaryActivity.java   View Source Code Vote up 5 votes
private void requestPermission() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(CustomDictionaryActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        } else {
            ActivityCompat.requestPermissions(CustomDictionaryActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
        }
    }
 
Example 14
Project: GeoFencer   File: GeoLocationUtil.java   View Source Code Vote up 5 votes
public static void askForLocationPermission(Activity thisActivity, int requestCode) {
    if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                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.

            // we force to allow this permission
            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    requestCode);

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    requestCode);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
}
 
Example 15
Project: Linphone4Android   File: AssistantActivity.java   View Source Code Vote up 5 votes
public void checkAndRequestPermission(String permission, int result) {
	int permissionGranted = getPackageManager().checkPermission(permission, getPackageName());
	Log.i("[Permission] " + permission + " is " + (permissionGranted == PackageManager.PERMISSION_GRANTED ? "granted" : "denied"));

	if (permissionGranted != PackageManager.PERMISSION_GRANTED) {
		if (LinphonePreferences.instance().firstTimeAskingForPermission(permission) || ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
			Log.i("[Permission] Asking for " + permission);
			ActivityCompat.requestPermissions(this, new String[]{permission}, result);
		}
	}
}
 
Example 16
Project: Self-Driving-Car   File: MapsActivity.java   View Source Code Vote up 5 votes
private boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Asking user if explanation is needed
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                android.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.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}
 
Example 17
Project: android-mrz-reader   File: CaptureActivity.java   View Source Code Vote up 5 votes
private void requestPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    PERMISSIONS_REQUEST_CAMERA);
        }
    }
}
 
Example 18
Project: GoogleMapsApp   File: MapsActivity.java   View Source Code Vote up 5 votes
public boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Asking user if explanation is needed
        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.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);


        } 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;
    }
}
 
Example 19
Project: Linphone4Android   File: CallIncomingActivity.java   View Source Code Vote up 5 votes
private void checkAndRequestCallPermissions() {
	ArrayList<String> permissionsList = new ArrayList<String>();
	
	int recordAudio = getPackageManager().checkPermission(Manifest.permission.RECORD_AUDIO, getPackageName());
	Log.i("[Permission] Record audio permission is " + (recordAudio == PackageManager.PERMISSION_GRANTED ? "granted" : "denied"));
	int camera = getPackageManager().checkPermission(Manifest.permission.CAMERA, getPackageName());
	Log.i("[Permission] Camera permission is " + (camera == PackageManager.PERMISSION_GRANTED ? "granted" : "denied"));
	
	if (recordAudio != PackageManager.PERMISSION_GRANTED) {
		if (LinphonePreferences.instance().firstTimeAskingForPermission(Manifest.permission.RECORD_AUDIO) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO)) {
			Log.i("[Permission] Asking for record audio");
			permissionsList.add(Manifest.permission.RECORD_AUDIO);
		}
	}
	if (LinphonePreferences.instance().shouldInitiateVideoCall() || LinphonePreferences.instance().shouldAutomaticallyAcceptVideoRequests()) {
		if (camera != PackageManager.PERMISSION_GRANTED) {
			if (LinphonePreferences.instance().firstTimeAskingForPermission(Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
				Log.i("[Permission] Asking for camera");
				permissionsList.add(Manifest.permission.CAMERA);
			}
		}
	}
	
	if (permissionsList.size() > 0) {
		String[] permissions = new String[permissionsList.size()];
		permissions = permissionsList.toArray(permissions);
		ActivityCompat.requestPermissions(this, permissions, 0);
	}
}
 
Example 20
Project: firebase-ui-quickstart-android   File: Utility.java   View Source Code Vote up 4 votes
public static boolean shouldShowReadStorageRequestPermissionRationale(AppCompatActivity activity) {
    return ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
}