Android Marshmallow权限错误

时间:2016-04-18 12:08:17

标签: android android-6.0-marshmallow

我正在为我的应用添加某些权限以允许访问相机,例如一切正常。但当我最小化应用程序并禁用权限然后打开应用程序时,应用程序崩溃而不要求我重新启用权限(直到我关闭应用程序然后打开它)。如何修复此错误,以便应用程序不会崩溃并再次请求权限或安全重新启动以请求权限。

这是我的代码

主要活动:

的onCreate:

 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    GlobalVariables.MY_PERMISSIONS_REQUEST_CAMERA);
        }


@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode)
        {

            case GlobalVariables.MY_PERMISSIONS_REQUEST_CAMERA:
            {
                if (grantResults.length <= 0
                        || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    globalVariables.ShowOKAlert("Error","Please Accept All Requested Permissions or the app wont function properly",this,false);
                }
                return;
            }
        }
    }

该活动实现了ActivityCompat.OnRequestPermissionsResultCallback

3 个答案:

答案 0 :(得分:1)

第1步:

 String[] permissions = new String[]{ 
    Manifest.permission.INTERNET,
    Manifest.permission.READ_PHONE_STATE,
    Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE,
 };

第2步: -

private boolean checkPermissions() {
    int result;
    List<String> listPermissionsNeeded = new ArrayList<>();
    for (String p : permissions) {
        result = ContextCompat.checkSelfPermission(this, p);
        if (result != PackageManager.PERMISSION_GRANTED) {
             listPermissionsNeeded.add(p);
        }
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 100);
        return false;
    }
    return true;
}

第3步:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (requestCode == 100) {
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // do something
        }
        return;
    }
}

答案 1 :(得分:0)

您正在onCreate检查该权限。当您再次最小化并最大化应用程序时,不会调用该方法。

也许您应该在需要的地点检查该权限,或者至少在onResume中检查,当您重新打开应用时,肯定会再次调用该权限。

onResume中调用它可确保应用程序继续工作,如果用户在屏幕已拆分的另一个窗口中禁用该权限(Android N功能)。

答案 2 :(得分:0)

使用ActivityCompat

的直接静态方法实现此目的的简便方法
public static void requestPermissions(final @NonNull Activity activity,
            final @NonNull String[] permissions, final int requestCode)

您可以在此处查看此

所需的参数

-- Activity Instance

-- String array of permissions

-- a user defined request code

<强>用法

定义全局变量

//Run-time permissions for MarshMallow +
    public final String[] PERMISSION_ALL = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    //validateRequestPermissionsRequestCode requires requestCode to be of 8 bits, i.e. range: 0-255.
    public final int PERMISSION_REQUEST_CODE = 100;

最后在您的活动oncreate()方法中,您可以调用上面提到的静态函数

public class MainActivity extends AppCompatActivity
{
    //your code here

     @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        //do other code work

        super.onCreate(savedInstanceState);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.M)
        {
            ActivityCompat.requestPermissions(this, PERMISSION_ALL, PERMISSION_REQUEST_CODE);
        }

        //do other code work
    }

    //your code here
}

你已经完成了......

有关运行时Android权限的更多信息Click here