本文整理了Java中android.app.Activity.requestPermissions()
方法的一些代码示例,展示了Activity.requestPermissions()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Activity.requestPermissions()
方法的具体详情如下:
包路径:android.app.Activity
类名称:Activity
方法名:requestPermissions
暂无
代码示例来源:origin: pili-engineering/PLDroidPlayer
@Override
public void onClick(DialogInterface dialog, int which) {
mActivity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
});
代码示例来源:origin: naman14/Timber
public static void askForPermission(Activity activity, String[] permissions, PermissionCallback permissionCallback) {
if (permissionCallback == null) {
return;
}
if (hasPermission(activity, permissions)) {
permissionCallback.permissionGranted();
return;
}
PermissionRequest permissionRequest = new PermissionRequest(new ArrayList<String>(Arrays.asList(permissions)), permissionCallback);
permissionRequests.add(permissionRequest);
activity.requestPermissions(permissions, permissionRequest.getRequestCode());
}
代码示例来源:origin: aa112901/remusic
public static void askForPermission(Activity activity, String[] permissions, PermissionCallback permissionCallback) {
if (permissionCallback == null) {
return;
}
if (hasPermission(activity, permissions)) {
permissionCallback.permissionGranted();
return;
}
PermissionRequest permissionRequest = new PermissionRequest(new ArrayList<String>(Arrays.asList(permissions)), permissionCallback);
permissionRequests.add(permissionRequest);
activity.requestPermissions(permissions, permissionRequest.getRequestCode());
}
代码示例来源:origin: google/ExoPlayer
/**
* Checks whether it's necessary to request the {@link permission#READ_EXTERNAL_STORAGE}
* permission read the specified {@link Uri}s, requesting the permission if necessary.
*
* @param activity The host activity for checking and requesting the permission.
* @param uris {@link Uri}s that may require {@link permission#READ_EXTERNAL_STORAGE} to read.
* @return Whether a permission request was made.
*/
@TargetApi(23)
public static boolean maybeRequestReadExternalStoragePermission(Activity activity, Uri... uris) {
if (Util.SDK_INT < 23) {
return false;
}
for (Uri uri : uris) {
if (Util.isLocalFileUri(uri)) {
if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(new String[] {permission.READ_EXTERNAL_STORAGE}, 0);
return true;
}
break;
}
}
return false;
}
代码示例来源:origin: pili-engineering/PLDroidPlayer
mActivity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
代码示例来源:origin: airbnb/AirMapView
/**
* Check if any location permissions are granted, and invoke onLocationPermissionsGranted() in the
* callback if granted. It will ask users for location permissions and invoke the same callback if
* no permission was granted and got granted at runtime.
*
* @param airMapInterface the callback interface if permission is granted.
*/
static boolean checkLocationPermissions(Activity targetActivity,
AirMapInterface airMapInterface) {
if (hasSelfPermissions(targetActivity, LOCATION_PERMISSIONS)) {
airMapInterface.onLocationPermissionsGranted();
return true;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
targetActivity.requestPermissions(LOCATION_PERMISSIONS, LOCATION_PERMISSION_REQUEST_CODE);
}
//else don't have location permissions in pre M, don't do anything.
return false;
}
代码示例来源:origin: north2016/T-MVP
mRequestCode = requestCode;
if (object instanceof Activity) {
((Activity) object).requestPermissions(deniedPermissions
.toArray(new String[deniedPermissions.size()]), requestCode);
} else if (object instanceof android.app.Fragment) {
代码示例来源:origin: googlesamples/easypermissions
@Test
public void shouldRequestPermissions_whenPositiveButtonFromActivity() {
RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig,
permissionCallbacks, rationaleCallbacks);
listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE);
verify(activity, times(1)).requestPermissions(PERMS, REQUEST_CODE);
}
代码示例来源:origin: robolectric/robolectric
@Test
@Config(minSdk = M)
public void getPermission_shouldReturnRequestedPermissions() {
// GIVEN
String[] permission = {Manifest.permission.CAMERA};
int requestCode = 1007;
Activity activity = Robolectric.setupActivity(Activity.class);
// WHEN
activity.requestPermissions(permission, requestCode);
// THEN
ShadowActivity.PermissionsRequest request = shadowOf(activity).getLastRequestedPermission();
assertThat(request.requestCode).isEqualTo(requestCode);
assertThat(request.requestedPermissions).isEqualTo(permission);
}
代码示例来源:origin: pili-engineering/PLDroidShortVideo
@Override
public void onClick(DialogInterface dialog, int which) {
mActivity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
});
代码示例来源:origin: DaxiaK/MyDiary
@TargetApi(Build.VERSION_CODES.M)
public static boolean checkPermission(Activity activity, final int requestCode) {
switch (requestCode) {
case REQUEST_WRITE_ES_PERMISSION:
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
requestCode);
return false;
} else {
activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
requestCode);
return false;
}
}
break;
}
return true;
}
代码示例来源:origin: lovedise/PermissionGen
@TargetApi(value = Build.VERSION_CODES.M)
private static void requestPermissions(Object object, int requestCode, String[] permissions){
if(!Utils.isOverMarshmallow()) {
doExecuteSuccess(object, requestCode);
return;
}
List<String> deniedPermissions = Utils.findDeniedPermissions(getActivity(object), permissions);
if(deniedPermissions.size() > 0){
if(object instanceof Activity){
((Activity)object).requestPermissions(deniedPermissions.toArray(new String[deniedPermissions.size()]), requestCode);
} else if(object instanceof Fragment){
((Fragment)object).requestPermissions(deniedPermissions.toArray(new String[deniedPermissions.size()]), requestCode);
} else {
throw new IllegalArgumentException(object.getClass().getName() + " is not supported");
}
} else {
doExecuteSuccess(object, requestCode);
}
}
代码示例来源:origin: pili-engineering/PLDroidShortVideo
mActivity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
代码示例来源:origin: UncorkedStudios/recordablesurfaceview
/**
* Check to see we have the necessary permissions for this app, and ask for them if we don't.
*/
public static void requestPermissions(Activity activity) {
activity.requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE_PERMISSION, RECORD_AUDIO_PERMISSION},
GRANT_REQUEST_CODE);
}
代码示例来源:origin: wj576038874/PhotoSelector
@RequiresApi(api = Build.VERSION_CODES.M)
public static boolean checkWriteStoragePermission(Activity activity) {
int writeStoragePermissionState =
ContextCompat.checkSelfPermission(activity, WRITE_EXTERNAL_STORAGE);
boolean writeStoragePermissionGranted = writeStoragePermissionState == PackageManager.PERMISSION_GRANTED;
if (!writeStoragePermissionGranted) {
activity.requestPermissions(PermissionsConstant.PERMISSIONS_EXTERNAL_WRITE,
PermissionsConstant.REQUEST_EXTERNAL_WRITE);
}
return writeStoragePermissionGranted;
}
代码示例来源:origin: fg607/RelaxFinger
public static boolean checkPermissionGranted(Activity activity, String permission) {
if(Build.VERSION.SDK_INT>22){
int grant = activity.checkSelfPermission(permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
activity.requestPermissions(new String[]{permission}, 123);
return false;
}
}
return true;
}
代码示例来源:origin: xuningjack/RequestPermission
public static void requestPermissions(Activity activity, String[] permissions,
int requestCode) {
if (activity instanceof RequestPermissionsRequestCodeValidator) {
((RequestPermissionsRequestCodeValidator) activity)
.validateRequestPermissionsRequestCode(requestCode);
}
activity.requestPermissions(permissions, requestCode);
}
代码示例来源:origin: adorsys/sms-parser-android
@RequiresApi(api = Build.VERSION_CODES.M)
public static void requestSMSPermission(@NonNull Activity activity) {
final String permission = Manifest.permission.RECEIVE_SMS;
int hasSpecificPermission = ContextCompat.checkSelfPermission(activity, permission);
if (hasSpecificPermission != PackageManager.PERMISSION_GRANTED
&& !activity.shouldShowRequestPermissionRationale(permission)) {
activity.requestPermissions(new String[]{permission},
REQUEST_CODE_ASK_PERMISSIONS);
}
}
}
代码示例来源:origin: VREMSoftwareDevelopment/WiFiAnalyzer
void check() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (activity.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
activity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
return;
}
if (activity.isFinishing()) {
return;
}
activity.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE);
}
}
代码示例来源:origin: VREMSoftwareDevelopment/WiFiAnalyzer
@Test
public void testCheckWithFineLocationGranted() {
// setup
when(activity.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)).thenReturn(PackageManager.PERMISSION_DENIED);
when(activity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)).thenReturn(PackageManager.PERMISSION_GRANTED);
// execute
fixture.check();
// validate
verify(activity).checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
verify(activity).checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
verify(activity, never()).isFinishing();
verify(activity, never()).requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PermissionChecker.REQUEST_CODE);
}
内容来源于网络,如有侵权,请联系作者删除!