问题描述
我正在尝试从用户那里获得相机许可,然后再启动我的相机活动.用户在权限对话框中按允许"后,永远不会再调用onRequestPermissionsResult.这是我的活动课:
I am trying to get the camera permission from the user before launching my camera activity. onRequestPermissionsResult is never called back after the user presses "Allow" from the permission dialog. Here is my Activity class:
public class ImageCaptureActivity extends AppCompatActivity {
public static final String TAG = ImageCaptureActivity.class.getSimpleName();
private static final int REQUEST_CAMERA = 0;
private static final int REQUEST_CAMERA_PERMISSION = 1;
private Point mSize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_capture);
Display display = getWindowManager().getDefaultDisplay();
mSize = new Point();
display.getSize(mSize);
// Request for Camera Permission
requestForCameraPermission(findViewById(android.R.id.content));
}
/**
* @param view
* @brief requestForCameraPermission
*/
public void requestForCameraPermission(View view) {
Log.v(TAG, "Requesting Camera Permission");
final String permission = Manifest.permission.CAMERA;
if (ContextCompat.checkSelfPermission(ImageCaptureActivity.this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(ImageCaptureActivity.this, permission)) {
showPermissionRationaleDialog(getString(R.string.camera_permission_rationale), permission);
} else {
requestForPermission(permission);
}
} else {
launch();
}
}
/**
* @param message
* @param permission
* @brief showPermissionRationaleDialog
*/
private void showPermissionRationaleDialog(final String message, final String permission) {
new AlertDialog.Builder(ImageCaptureActivity.this)
.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ImageCaptureActivity.this.requestForPermission(permission);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}
/**
* @param permission
* @brief requestForPermission
*/
private void requestForPermission(final String permission) {
ActivityCompat.requestPermissions(ImageCaptureActivity.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION);
}
/**
* @brief launch
*/
private void launch() {
Log.v(TAG, "Camera Permission Granted, launching the CameraActivity");
String documentId = getIntent().getStringExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID);
Intent startCustomCameraIntent = new Intent(this, CameraActivity.class);
startCustomCameraIntent.putExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID, documentId);
startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA_PERMISSION:
final int numOfRequest = grantResults.length;
final boolean isGranted = numOfRequest == 1
&& PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
Log.v(TAG, "Camera Permission callback on onRequestPermissionsResult");
if (isGranted) {
launch();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
这是我的带有相机许可的应用清单:
Here is my App Manifest with the Camera Permission:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.testpackage">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="true" />
<uses-feature android:name="android.hardware.camera2" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
我的Gradle依赖项:
My Gradle Dependency:
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:support-v4:24.0.0'
推荐答案
我亲自尝试了您的代码,一切正常.我认为唯一可能成为问题的是您的活动具有以下两个标志的情况:
I tried your code myself and everything works just fine. I think that the only thing that can be a problem is the scenario when your activity has these two flags:
android:noHistory="true"
android:excludeFromRecents="true"
这是一个可以解释问题的人: https://stackoverflow.com/a/35772265/5281581
Here's a guy that is explains the issue: https://stackoverflow.com/a/35772265/5281581
这篇关于在Android活动中未回调onRequestPermissionsResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!