java.lang.StackOverflowError 请求运行时权限时

java.lang.StackOverflowError when request for runtime permission

我创建了一个公共 class PermissionManager 用于从一个地方管理所有权限,正常情况下它工作正常但上传后它显示崩溃分析的错误报告我无法重现,详情见下文

Fatal Exception: java.lang.WhosebugError: stack size 8MB
       at android.content.res.Resources.getText(Resources.java:308)
       at android.content.res.Resources.getString(Resources.java:400)
       at android.content.Context.getString(Context.java:409)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
       at android.app.Activity.requestPermissions(Activity.java:3857)
       at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
       at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
       at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
       at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
       at com.mypackage.activities.SplashActivity.onCreate(SplashActivity.java:53)
       at android.app.Activity.performCreate(Activity.java:6285)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
       at android.app.ActivityThread.access0(ActivityThread.java:150)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:171)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

SplashActivity.java

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (PermissionManager.checkLocationPermission(this)) {

              // do the task
            }
    }

     @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PermissionManager.MY_PERMISSIONS_REQUEST_LOCATION_ACCESS: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // do the task
                } else {
//                    Toast.makeText(getApplicationContext(), "For find better result,enable GPS permission from setting", Toast.LENGTH_LONG).show();
                    if (!PermissionManager.MY_REQUESTED_DIALOG) {
                        PermissionManager.checkLocationPermission(this);
                    } else {
                        startNextActivity(0);
                    }
                }


            }
        }
    }

PermissionManager.java

public class PermissionManager {

    public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 101;
    public static final int MY_PERMISSIONS_REQUEST_LOCATION_ACCESS = 102;
    public static final int MY_PERMISSIONS_REQUEST_CAMERA = 103;
    public static final int MY_PERMISSIONS_REQUEST_CALL = 104;
    public static final int MY_PERMISSIONS_REQUEST_AUDIO = 105;

    public static boolean MY_REQUESTED_DIALOG = false;


    private static String[] storagePermission = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static String[] cameraPermission = new String[]{Manifest.permission.CAMERA};
    private static String[] locationPermission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
    private static String[] phoneCallPermission = new String[]{Manifest.permission.CALL_PHONE};
    private static String[] recordAudioCallPermission = new String[]{Manifest.permission.RECORD_AUDIO};


    public static boolean checkPhoneCallPermission(final Activity context) {
        return checkPermission(context, phoneCallPermission, "Permission necessary", "Allow call permission", MY_PERMISSIONS_REQUEST_CALL);
    }


    public static boolean checkStoragePermission(final Activity context) {
        return checkPermission(context, storagePermission, "", context.getString(R.string.storage_permission_rejected), MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
    }

    public static boolean checkLocationPermission(final Activity context) {
        return checkPermission(context, locationPermission, "", context.getString(R.string.location_permission_rejected), MY_PERMISSIONS_REQUEST_LOCATION_ACCESS);
    }

    public static boolean checkRecordAudioPermission(final Activity context) {
        return checkPermission(context, recordAudioCallPermission, "", context.getString(R.string.audio_permission_rejected), MY_PERMISSIONS_REQUEST_AUDIO);
    }


    /*
     *
     * Check the permission
     *
     * */

    private static boolean checkPermission(final Activity context, String[] permission, String messageTitle, String messageDetails, int requestCode) {

        MY_REQUESTED_DIALOG = false;
        if (checkAPIVersion()) {
            if (checkPermissionStatus(context, permission)) {
                if (shouldShowRequestPermission(context, permission)) {
                    MY_REQUESTED_DIALOG = true;
                    showDialogForPermission(context, messageTitle, messageDetails, permission, requestCode);
                } else {
                    requestPermission(context, permission, requestCode);
                }
                return false;
            } else {
                return true;
            }
        }
        return true;

    }

    /*
     *
     * Show dialog for why ,this permission is needed
     *
     * */
    private static void showDialogForPermission(final Activity context, String title, String message, final String[] requestedPermission, final int requestCode) {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
        alertBuilder.setCancelable(false);
        alertBuilder.setTitle(title);
        alertBuilder.setMessage(message);
        alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                requestPermission(context, requestedPermission, requestCode);
            }
        });
        AlertDialog alert = alertBuilder.create();
        alert.show();
    }

    /*
     *
     * Request for permission
     *
     * */
    private static void requestPermission(Activity context, String[] requestedPermission, int requestCode) {
        ActivityCompat.requestPermissions(context, requestedPermission, requestCode);
    }

    /*
     *
     * Check need to display dialog for permission access
     * executed when first time user decline the permission
     *
     * */
    private static boolean shouldShowRequestPermission(Activity context, String[] permissionCheck) {
        for (String permission : permissionCheck) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(context, permission))
                return true;
        }

        return false;
    }

    /*
     *
     * Check list of permission is granted or not
     * if not requested for permission
     *
     * */
    private static boolean checkPermissionStatus(Activity context, String[] permissionCheck) {
        for (String permission : permissionCheck) {
            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED)
                return true;
        }
        return false;
    }

    /*
     *
     * Check API version
     * if it's >=M need to check run time permission
     * */
    private static boolean checkAPIVersion() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
    }

}

谁能帮忙解决这个问题?

堆栈跟踪是无限循环的征兆。

你有一个无限循环,我猜这是由于有人拒绝了位置权限,并选择了 "Never ask agan" 复选框:

这将自动拒绝对位置权限的任何进一步请求。在您的情况下,这成为一个大问题,因为 PermissionManager::checkPermission() 提示使用 showDialogForPermission() 的权限对话框,该对话框会自动拒绝,进而调用 onRequestPermissionsResult().

if (shouldShowRequestPermission(context, permission)) {
    MY_REQUESTED_DIALOG = true;
    showDialogForPermission(context, messageTitle, messageDetails, permission, requestCode);
} else {
    /* THIS LINE IS STARTING THE INFINITE LOOP */
    requestPermission(context, permission, requestCode); 
}

因为 shouldShowRequestPermission(context, permission) 正在返回 false,所以 MY_REQUESTED_DIALOG 永远不会设置为 true 并且该过程一遍又一遍地循环。如果 shouldShowRequestPermission() == false 您的许可在 onRequestPermissionsResult().

中都被拒绝,我的建议是中断许可流程
if (shouldShowRequestPermission(context, permission)) {
    MY_REQUESTED_DIALOG = true;
    showDialogForPermission(context, messageTitle, messageDetails, permission, requestCode);
} else {
    // Gracefully handle the fact that Location permission will never be granted
}