Java Code Examples for android.app.Activity#shouldShowRequestPermissionRationale()

The following examples show how to use android.app.Activity#shouldShowRequestPermissionRationale() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
static boolean checkStoragePermission(Activity activity, int requestCode) {
	if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
		// no need to check
		return true;
	}
	final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
	if (PackageManager.PERMISSION_GRANTED != activity.checkSelfPermission(permission)) {
		if (!activity.shouldShowRequestPermissionRationale(permission)) {
			activity.requestPermissions(new String[] {permission}, requestCode);
		} else {
			showToast(activity, R.string.warning_storage_permission);
		}
		return false;
	} else {
		return true;
	}
}
 
Example 2
Source Project: XXPermissions   File: PermissionUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
     * 检查某个权限是否被永久拒绝
     *
     * @param activity              Activity对象
     * @param permission            请求的权限
     */
    private static boolean checkSinglePermissionPermanentDenied(Activity activity, String permission) {

//        // 安装权限和浮窗权限不算,本身申请方式和危险权限申请方式不同,因为没有永久拒绝的选项,所以这里返回false
//        if (Permission.REQUEST_INSTALL_PACKAGES.equals(permission) || Permission.SYSTEM_ALERT_WINDOW.equals(permission)) {
//            return false;
//        }

        // 检测8.0的两个新权限
        if (Permission.ANSWER_PHONE_CALLS.equals(permission) || Permission.READ_PHONE_NUMBERS.equals(permission)) {

            // 检查当前的安卓版本是否符合要求
            if (!isOverOreo()) {
                return false;
            }
        }

        if (isOverMarshmallow()) {
            if (activity.checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED  &&
                    !activity.shouldShowRequestPermissionRationale(permission)) {
                return true;
            }
        }
        return false;
    }
 
Example 3
@Override
public boolean canRequestPermission(String permission) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false;

    Activity activity = getActivity().get();
    if (activity == null) return false;

    if (isPermissionRevokedByPolicy(permission)) {
        return false;
    }

    if (activity.shouldShowRequestPermissionRationale(permission)) {
        return true;
    }

    // Check whether we have ever asked for this permission by checking whether we saved
    // a preference associated with it before.
    String permissionQueriedKey = getHasRequestedPermissionKey(permission);
    SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
    if (!prefs.getBoolean(permissionQueriedKey, false)) return true;

    return false;
}
 
Example 4
@TargetApi(M)
static private void getPermission(String[] permissions, Activity activity) {
    if(Build.VERSION.SDK_INT>=23) {
        ArrayList<String> preToDo = new ArrayList<>();
        boolean tip = false;
        for (String pre : permissions) {
            if (activity.checkSelfPermission(pre) != PackageManager.PERMISSION_GRANTED) {
                preToDo.add(pre);
                if (activity.shouldShowRequestPermissionRationale(pre)) {
                    tip = true;
                }
            }
        }
        if (preToDo.size() == 0)
            return;
        if (tip)
            showToast(sContext.getString(R.string.permissions_need));
        activity.requestPermissions(preToDo.toArray(new String[preToDo.size()]), 0);
    }
}
 
Example 5
/**
 * Determine if user has already been asked these permissions and denied us the ability to
 * ask again. If true, send them to system to grant required permissions.
 *
 * @param activity reference to activity we are checking
 * @return true if we cannot ask for the necessary permissions
 */
private static boolean shouldSendToSystemPrefsForRequiredPermissions(Activity activity) {
    int numTimesAsked = getSharedPreferences(activity)
            .getInt(PREF_NUM_TIMES_ASKED_REQUIRED_PERMISSIONS, 0);
    if (numTimesAsked > 1) {
        for (String permission : REQUIRED_PERMISSIONS) {
            if (ContextCompat.checkSelfPermission(activity, permission)
                    == PackageManager.PERMISSION_DENIED &&
                    !activity.shouldShowRequestPermissionRationale(permission)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 6
@Override
public void requestPermission(final Activity activity) {
    if (canAskPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity.checkSelfPermission(
                    Manifest.permission.ACCESS_FINE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED) {

                if (activity.shouldShowRequestPermissionRationale(
                        Manifest.permission.ACCESS_FINE_LOCATION)) {
                    AlertDialog.Builder builder =
                            new AlertDialog.Builder(activity);
                    builder.setMessage(activity.getString(R.string.permissions_location));
                    builder.setPositiveButton(android.R.string.ok,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    activity.requestPermissions(new String[]{
                                                    Manifest.permission.ACCESS_FINE_LOCATION},
                                            FINE_LOCATION_PERMISSION_REQUESTCODE);
                                }
                            }
                    );
                    // display the dialog
                    builder.create().show();
                } else {
                    // request permission
                    activity.requestPermissions(
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            FINE_LOCATION_PERMISSION_REQUESTCODE);
                }
            }
        }
    }
}
 
Example 7
Source Project: PixImagePicker   File: PermUtil.java    License: Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.M)
private static boolean addPermission(List<String> permissionsList, String permission, Activity ac) {
    if (ac.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
        permissionsList.add(permission);
        // Check for Rationale Option
        return ac.shouldShowRequestPermissionRationale(permission);
    }
    return true;
}
 
Example 8
@TargetApi(Build.VERSION_CODES.M)
private boolean shouldShowRequestPermissionRationaleImplementation(final Activity activity, final String... permissions) {
    for (String p : permissions) {
        if (!isGranted(p) && !activity.shouldShowRequestPermissionRationale(p)) {
            return false;
        }
    }
    return true;
}
 
Example 9
Source Project: RxPermissions   File: RxPermissions.java    License: Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
private boolean shouldShowRequestPermissionRationaleImplementation(final Activity activity, final String... permissions) {
    for (String p : permissions) {
        if (!isGranted(p) && !activity.shouldShowRequestPermissionRationale(p)) {
            return false;
        }
    }
    return true;
}
 
Example 10
Source Project: AndroidProjects   File: RxPermissions.java    License: MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
private boolean shouldShowRequestPermissionRationaleImplementation(final Activity activity, final String... permissions) {
    for (String p : permissions) {
        if (!isGranted(p) && !activity.shouldShowRequestPermissionRationale(p)) {
            return false;
        }
    }
    return true;
}
 
Example 11
public static boolean shouldShowRequestPermissionRationale(Activity context) {
    return context.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION);
}
 
Example 12
@Override
public void requestPermission(final Activity activity) {
    if (canAskPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity.checkSelfPermission(
                    Manifest.permission.SEND_SMS) !=
                    PackageManager.PERMISSION_GRANTED) {

                if (canAskPermission) {
                    if (activity.shouldShowRequestPermissionRationale(
                            Manifest.permission.SEND_SMS)) {
                        AlertDialog.Builder builder =
                                new AlertDialog.Builder(activity);
                        builder.setMessage(activity.getString(R.string.permissions_send_sms));
                        builder.setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (canAskPermission) {
                                            activity.requestPermissions(new String[]{
                                                            Manifest.permission.SEND_SMS},
                                                    SENDSMS_PERMISSION_REQUESTCODE);
                                        }
                                    }
                                }
                        );
                        // display the dialog
                        builder.create().show();
                    } else {
                        // request permission
                        if (canAskPermission) {
                            activity.requestPermissions(
                                    new String[]{Manifest.permission.SEND_SMS},
                                    SENDSMS_PERMISSION_REQUESTCODE);
                        }
                    }
                }
            }
        }
    }
}
 
Example 13
@Override
public void requestPermission(final Activity activity) {
    if (canAskPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity.checkSelfPermission(
                    Manifest.permission.FOREGROUND_SERVICE) !=
                    PackageManager.PERMISSION_GRANTED) {

                if (canAskPermission) {
                    if (activity.shouldShowRequestPermissionRationale(
                            Manifest.permission.FOREGROUND_SERVICE)) {
                        AlertDialog.Builder builder =
                                new AlertDialog.Builder(activity);
                        builder.setMessage(activity.getString(R.string.permissions_foreground_service));
                        builder.setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (canAskPermission) {
                                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                                                activity.requestPermissions(new String[]{
                                                                Manifest.permission.FOREGROUND_SERVICE},
                                                        FOREGROUND_SERVICE_PERMISSION_REQUESTCODE);
                                            }
                                        }
                                    }
                                }
                        );
                        // display the dialog
                        builder.create().show();
                    } else {
                        // request permission
                        if (canAskPermission) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                                activity.requestPermissions(
                                        new String[]{Manifest.permission.FOREGROUND_SERVICE},
                                        FOREGROUND_SERVICE_PERMISSION_REQUESTCODE);
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example 14
@Override
public void requestPermission(final Activity activity) {
    if (canAskPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity.checkSelfPermission(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                    PackageManager.PERMISSION_GRANTED) {

                if (canAskPermission) {
                    if (activity.shouldShowRequestPermissionRationale(
                            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                        AlertDialog.Builder builder =
                                new AlertDialog.Builder(activity);
                        builder.setMessage(activity.getString(R.string.permissions_write_disk));
                        builder.setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (canAskPermission) {
                                            activity.requestPermissions(new String[]{
                                                            Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                                    WRITE_EXTERNAL_STORAGE_PERMISSION_REQUESTCODE);
                                        }
                                    }
                                }
                        );
                        // display the dialog
                        builder.create().show();
                    } else {
                        // request permission
                        if (canAskPermission) {
                            activity.requestPermissions(
                                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                    WRITE_EXTERNAL_STORAGE_PERMISSION_REQUESTCODE);
                        }
                    }
                }
            }
        }
    }
}
 
Example 15
@Override
public void requestPermission(final Activity activity) {
    if (canAskPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity.checkSelfPermission(
                    Manifest.permission.GET_ACCOUNTS) !=
                    PackageManager.PERMISSION_GRANTED) {

                if (canAskPermission) {
                    if (activity.shouldShowRequestPermissionRationale(
                            Manifest.permission.GET_ACCOUNTS)) {
                        AlertDialog.Builder builder =
                                new AlertDialog.Builder(activity);
                        builder.setMessage(activity.getString(R.string.permissions_accounts));
                        builder.setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (canAskPermission) {
                                            activity.requestPermissions(new String[]{
                                                            Manifest.permission.GET_ACCOUNTS},
                                                    GET_ACCOUNTS_PERMISSION_REQUESTCODE);
                                        }
                                    }
                                }
                        );
                        // display the dialog
                        builder.create().show();
                    } else {
                        // request permission
                        if (canAskPermission) {
                            activity.requestPermissions(
                                    new String[]{Manifest.permission.GET_ACCOUNTS},
                                    GET_ACCOUNTS_PERMISSION_REQUESTCODE);
                        }
                    }
                }
            }
        }
    }
}
 
Example 16
private boolean shouldShowRequestPermissionRationale(Activity activity, String permission) {
  if (Build.VERSION.SDK_INT >= 23) {
    return activity.shouldShowRequestPermissionRationale(permission);
  }
  return false;
}
 
Example 17
Source Project: Muzesto   File: Nammu.java    License: GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
public static boolean shouldShowRequestPermissionRationale(Activity activity, String permissions) {
    return activity.shouldShowRequestPermissionRationale(permissions);
}
 
Example 18
Source Project: MusicPlayer   File: Nammu.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static boolean shouldShowRequestPermissionRationale(Activity activity, String permissions) {
    return activity.shouldShowRequestPermissionRationale(permissions);
}
 
Example 19
public static boolean shouldShowRational(Activity activity, String permission) {
    if (useRunTimePermissions()) {
        return activity.shouldShowRequestPermissionRationale(permission);
    }
    return false;
}
 
Example 20
Source Project: letv   File: ActivityCompatApi23.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean shouldShowRequestPermissionRationale(Activity activity, String permission) {
    return activity.shouldShowRequestPermissionRationale(permission);
}