我有一个WebView,加载了一些页面需要GeolocationPermissions的权限。为此,我重写了onGeolocationPermissionsShowPrompt()。行。
此外,我的应用程序使用新的权限模型定位SDK 23(Android M)。因此,我需要检查用户是否授予权限ACCESS_FINE_LOCATION,如果没有,则显示一个询问权限的对话框。
如何仅针对需要GeolocationPermissions的页面显示该对话框?
不调用onPermissionRequest(PermissionRequest请求)回调。
AndroidManifest:
WebView回调
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, true);
super.onGeolocationPermissionsShowPrompt(origin, callback);
}
@Override
public void onPermissionRequest(PermissionRequest request) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
requestLocationPermission();
else {
super.onPermissionRequest(request);
}
显示对话框权限
private void requestLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
new ADPDialogFragment(getActivity()) {
@Override
public String getTitre() {
return getString(R.string.mob_he_configuration_dialogue_message_titre);
}
@Override
public String getMessage() {
return getString(R.string.permission_location_rationale);
}
@Override
public void doPositiveClick() {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
}
}.show(getFragmentManager(), TAG);
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
}
权限回调
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_LOCATION) {
// BEGIN_INCLUDE(permission_result)
// Received permission result for camera permission.
Log.i(TAG, "Received response for Camera permission request.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Camera permission has been granted, preview can be displayed
} else {
Toast.makeText(getActivity(), getString(R.string.permissions_not_granted), Toast.LENGTH_SHORT).show();
}
// END_INCLUDE(permission_result)
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
感谢您的帮助!
答案 0 :(得分:4)
您可以在调用onGeolocationPermissionsShowPrompt
时要求用户许可:
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
mCallback = callback;
mOrigin = origin;
requestLocationPermission();
} else {
callback.invoke(origin, true, true);
}
}
当用户选择授予您许可时:
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults)
{
switch (requestCode) {
case REQUEST_LOCATION:
if (mCallback != null && mOrigin != null)
mCallback.invoke(mOrigin,
grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED,
false);
mCallback = null;
mOrigin = null;
break;
default:
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
}