gpt4 book ai didi

java - 为什么我的应用程序显示两个 requestPermission Rationales?

转载 作者:太空狗 更新时间:2023-10-29 14:39:57 29 4
gpt4 key购买 nike

我对编程比较陌生,刚刚制作了一个相机应用程序,其预览窗口保存在表面 View 中,需要访问位置,显然还有相机。

但是,在显示一个权限的 requestPermissionRationale 并且用户同意后,第二个 requestPermissionRationale 出现,将相机预览卡住在原地。预览在离开应用程序并返回时起作用。为什么会这样?

我已经包含了请求权限的方法、我的表面 View 类和下面的 logcat。 requestUpdates() 包含实例化相机的代码,surfaceview.request()onCreate() 中调用。如下所示,在onResume()中调用了request,在onCreate()中也调用了request。

   private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@TargetApi(23)
protected void requestCam() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
showMessageOKCancel("This app requires access to the camera in order to function.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[] {Manifest.permission.CAMERA}, CAMPERMISSION_KEY);
}
});

} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMPERMISSION_KEY);
}

}
@TargetApi(23)
protected void requestLoc() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {

showMessageOKCancel("This app requires access to the location in order to function.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, LOCPERMISSION_KEY);
}

});

} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCPERMISSION_KEY);
}
}
@TargetApi(23)
protected void requestBoth() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
showMessageOKCancel("This app requires access to both the location and the camera in order to function.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA}, MULT_KEY);
requestPermissions(new String[] {Manifest.permission.CAMERA}, CAMPERMISSION_KEY);
}
});

} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA}, MULT_KEY);
}

}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
boolean needLoc = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED;
boolean needCam = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED;
switch (requestCode) {
case LOCPERMISSION_KEY:
if (needLoc == false && needCam == false) {
requestUpdates();
} else {
request();
}
break;
case CAMPERMISSION_KEY:
if (needCam == false && needLoc == false) {
requestUpdates();

} else {
request();
}
break;
case MULT_KEY:
if (needLoc == false && needCam == false) {
requestUpdates();

} else {
request();
}
}
}
@TargetApi(23)
public void request() {
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
requestUpdates();
} else if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
requestCam();
} else if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
requestLoc();
} else {
requestBoth();
}
}

public void onResume() {
request();
}
04-13 21:31:33.328 30847-30847/danielwei816.com.danger W/System.err: java.io.IOException: setPreviewTexture failed
04-13 21:31:33.328 30847-30847/danielwei816.com.danger W/System.err: at android.hardware.Camera.setPreviewSurface(Native Method)
04-13 21:31:33.328 30847-30847/danielwei816.com.danger W/System.err: at android.hardware.Camera.setPreviewDisplay(Camera.java:738)
04-13 21:31:33.328 30847-30847/danielwei816.com.danger W/System.err: at danielwei816.com.danger.ImageSurfaceView.surfaceCreated(ImageSurfaceView.java:29)
04-13 21:31:33.328 30847-30847/danielwei816.com.danger W/System.err: at android.view.SurfaceView.updateWindow(SurfaceView.java:634)
04-13 21:31:33.328 30847-30847/danielwei816.com.danger W/System.err: at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:162)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2281)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1299)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.view.Choreographer.doCallbacks(Choreographer.java:683)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.view.Choreographer.doFrame(Choreographer.java:619)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.os.Looper.loop(Looper.java:154)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6316)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
04-13 21:31:33.329 30847-30847/danielwei816.com.danger W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
04-13 21:31:33.387 30847-30847/danielwei816.com.danger E/Camera: Error 2

最佳答案

首先,您的代码中存在逻辑错误。您的权限检查与权限请求不匹配。请在代码中评论:

   public void request() {
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
requestUpdates();
} else if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
//You are checking for Location permission grant status but requesting camera permission
requestCam();
} else if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
//You are checking for Camera permission grant status but requesting location permission
requestLoc();
} else {
requestBoth();
}
}

之所以会看到两个理由如下:

您正在请求 onResume() 中的权限。

当您请求权限时,Android 操作系统会在内部启动一个新的 Activity 以显示权限对话框并覆盖您当前的 Activity。当用户授予权限时,您的 Activity 将进入前台并调用 onResume()。结果你会看到另一个理由

您可以通过仅在 onCreate() 中检查和请求(如有必要)权限并仅在授予权限后触发访问相机的请求来避免它。

关于java - 为什么我的应用程序显示两个 requestPermission Rationales?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49827175/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com