具有API<的Android PermissionsDispatcher 23

时间:2017-01-05 09:27:28

标签: java android permissions

我使用hotchemi PermissionsDispatcher来检查权限。它适用于API> = 23.但是当我尝试在具有API 16的设备上使用相同的代码时,我得到了一个FATAL EXCEPTION java.lang.StackOverflowError。 当我在生成的类showCameraWithCheck()

中调用MainActivityPermissionsDispatcher时,错误会增加

我深入研究了PermissionsDispatcher库生成的类MainActivityPermissionsDispatcher,并找到了Stack Overflow发生的原因。当我致电MainActivityPermissionsDispatcher.showCameraWithCheck(this);时,它会在生成的MainActivityPermissionsDispatcher

中转到此方法
static void showCameraWithCheck(MainActivity target) {
if (PermissionUtils.hasSelfPermissions(target, PERMISSION_SHOWCAMERA)) {
  target.showCamera();
} else {
  if (PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_SHOWCAMERA)) {
    target.showRationaleForCamera(new ShowCameraPermissionRequest(target));
  } else {
    ActivityCompat.requestPermissions(target, PERMISSION_SHOWCAMERA, REQUEST_SHOWCAMERA);
  }
}

}

事情是PermissionUtils.hasSelfPermissions(target, PERMISSION_SHOWCAMERA)总是如此,所以它调用target.showCamera()这是我的方法,它调用MainActivityPermissionsDispatcher.showCameraWithCheck(this);所以它进入无端循环....我猜hasSelfPermissions() 1}}方法检查它是否具有运行时权限(API> = 23)或旧样式权限(< API 23)。

  

E / AndroidRuntime:致命异常:主要                                                                                               java.lang.StackOverflowError的                                                                                                   在android.os.Parcel.readException(Parcel.java:1376)                                                                                                   在   android.app.ActivityManagerProxy.checkPermission(ActivityManagerNative.java:2874)                                                                                                   在android.app.ContextImpl.checkPermission(ContextImpl.java:1285)                                                                                                   在   android.content.ContextWrapper.checkPermission(ContextWrapper.java:421)                                                                                                   在   android.support.v4.content.PermissionChecker.checkPermission(PermissionChecker.java:89)                                                                                                   在   android.support.v4.content.PermissionChecker.checkSelfPermission(PermissionChecker.java:125)                                                                                                   在   permissions.dispatcher.PermissionUtils.hasSelfPermissions(PermissionUtils.java:73)                                                                                                   在   com.example.android.permissionsdispatcherapp.MainActivityPermissionsDispatcher.showCameraWithCheck(MainActivityPermissionsDispatcher.java:20)                                                                                                   在   com.example.android.permissionsdispatcherapp.MainActivity.showCamera(MainActivity.java:52)   ......................

@RuntimePermissions
public class MainActivity extends AppCompatActivity {
private static String LOG_TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {

        Button btnShowCamera = (Button) findViewById(R.id.btn_show_camera);
        btnShowCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(LOG_TAG, "Show camera button pressed.");
                showCamera();
            }

        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@NeedsPermission(Manifest.permission.CAMERA)
void showCamera() {
    // NOTE: delegate the permission handling to generated method
    // ERROR
    MainActivityPermissionsDispatcher.showCameraWithCheck(this);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    // NOTE: delegate the permission handling to generated method
    MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}

@OnPermissionDenied(Manifest.permission.CAMERA)
void onCameraDenied() {
    // NOTE: Deal with a denied permission, e.g. by showing specific UI
    // or disabling certain functionality
    Toast.makeText(this, R.string.permission_camera_denied, Toast.LENGTH_LONG).show();
}

@OnNeverAskAgain(Manifest.permission.CAMERA)
void onCameraNeverAskAgain() {
    Toast.makeText(this, R.string.permission_camera_never_askagain, Toast.LENGTH_LONG).show();
}

@OnShowRationale(Manifest.permission.CAMERA)
void showRationaleForCamera(PermissionRequest request) {
    // NOTE: Show a rationale to explain why the permission is needed, e.g. with a dialog.
    // Call proceed() or cancel() on the provided PermissionRequest to continue or abort
    showRationaleDialog(R.string.permission_camera_rationale, request);
}

private void showRationaleDialog(@StringRes int messageResId, final PermissionRequest request) {
    new AlertDialog.Builder(this)
            .setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(@NonNull DialogInterface dialog, int which) {
                    request.proceed();
                }
            })
            .setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(@NonNull DialogInterface dialog, int which) {
                    request.cancel();
                }
            })
            .setCancelable(false)
            .setMessage(messageResId)
            .show();
}
}

enter code here

1 个答案:

答案 0 :(得分:1)

找到了方法。我的代码错了。 它不需要调用showCamera()方法,因为生成的类MainActivityPermissionsDispatcher已经这样做了。由于API 16没有运行时权限,因此它以无结束循环结束。

这是修复:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
            Button btnShowCamera = (Button) findViewById(R.id.btn_show_camera);
            btnShowCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            Log.i(LOG_TAG, "Show camera button pressed.");
            askForPermission();
            }

        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

void askForPermission() {
    MainActivityPermissionsDispatcher.showCameraWithCheck(this);
}

@NeedsPermission(Manifest.permission.CAMERA)
void showCamera() {
    Toast.makeText(this, "Showing camera", Toast.LENGTH_LONG).show();
}