简体   繁体   English

使用ActivityCompat.requestPermissions寻求权限时,Android应用程序在6.0上崩溃

[英]Android app crashes on 6.0 while seeking permissions with ActivityCompat.requestPermissions

I've following MainActivity which seeks permission for 6.0 Phone & Storage tasks. 我关注的是MainActivity,它寻求6.0电话和存储任务的许可。 It prompts for permission 2x times and then crashes. 它提示输入2倍的权限,然后崩溃。 Do I need to re-start the MainActivity after I acquired_permission? 我获得获得许可后是否需要重新启动MainActivity? thanks for any help or pointers. 感谢您的帮助或指示。

MainActivity.java MainActivity.java

onCreate(){
seek_permissions()
//load db data and continue
//with the app tasks
}


public void seek_permissions(){
            boolean hasWritePermission = (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
            if (!hasWritePermission) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.MOUNT_FORMAT_FILESYSTEMS,Manifest.permission.RECEIVE_BOOT_COMPLETED,Manifest.permission.MODIFY_PHONE_STATE}, REQUEST_PERM);
            }        
    }
     @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_PERM:
            {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                acquired_perm = 1;
                }
                else {Toast.makeText(MainActivity.this, "Please consider granting these permissions", Toast.LENGTH_LONG).show(); }
            }
            return;
        } 
    }

For me this was caused by using android:noHistory="true" in my manifest for this activity? 对我来说,这是因为我在清单中为此活动使用了android:noHistory="true"

I believe this may be a bug with Android 6.0 as later versions work fine it seems. 我认为这可能是Android 6.0的一个错误,因为更高版本似乎可以正常工作。

If noHistory is set the ActivityCompat.requestPermissions call causes the onDestroy of the calling Activity to be executed thus destroying. 如果设置了noHistoryActivityCompat.requestPermissions调用将导致执行调用方Activity的onDestroy ,从而将其销毁。 As it isn't saved in the history due to the noHistory it no longer exists and the app is essentially closed when permission dialogue is dismissed. 由于由于noHistory而没有保存在历史记录中, noHistory它不再存在,并且在取消权限对话框时,该应用程序实际上已关闭。

As a workaround instead of "noHistory" in the manifest you can call finish() in onStop() or another appropriate location when navigating away from the Activity and remove the tag from your manifest. 作为解决方法,而不是清单中的"noHistory" ,您可以在离开Activity并从清单中删除标记时,在onStop()或其他适当的位置调用finish()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM