Android Marshmallow:在运行时更改权限会导致应用程序崩溃 [英] Android Marshmallow: Changing permissions at run time crashes app

查看:27
本文介绍了Android Marshmallow:在运行时更改权限会导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Marshmallow 重新设计了获取权限.所以在调用需要权限的方法之前处理权限并且它工作正常,但它在以下情况下崩溃:

Marshmallow has redesigned getting permissions. So Handled permissions before calling the method which needs permissions and it works fine, but It crashes in the following scenario:

第 1 步:打开应用并授予所有必要的权限

Step 1: Opened app and gave all the necessary permissions

第 2 步:点击主页按钮(所以应用程序在后台)

Step 2: Clicked Home button(So the app is in background)

第 3 步:在设置中手动更改权限

Step 3: Manually changed the permissions in the Settings

第 4 步:从多任务启动应用,现在由于应用上下文变得无效而崩溃

Step 4: Launched the app from multitasks, now it crashes because of app context becomes invalid

观察到应用再次创建,不明白为什么会发生这种情况.欢迎提出任何纠正此问题的建议!

Observed that app gets created again, don't understand why this happens. Any suggestions to rectify this issue would be welcome!

推荐答案

这是因为 Marshmallow 添加了附加功能.您需要在运行时向用户请求.为此,请使用我制作的这个类.然后在需要的地方使用它

It's because of additional features added from Marshmallow. You need to request from user at runtime. For this use this class which I have made. Then use it whereever required

public class AppPermission {

    public static boolean isMarshmallowPlusDevice() {
        return Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1;
    }

    @TargetApi(Build.VERSION_CODES.M)
    public static boolean isPermissionRequestRequired(Activity activity, @NonNull String[] permissions, int requestCode) {
        if (isMarshmallowPlusDevice() && permissions.length > 0) {
            List<String> newPermissionList = new ArrayList<>();
            for (String permission : permissions) {
                if (PackageManager.PERMISSION_GRANTED != activity.checkSelfPermission(permission)) {
                    newPermissionList.add(permission);
                }
            }
            if (newPermissionList.size() > 0) {
                activity.requestPermissions(newPermissionList.toArray(new String[newPermissionList.size()]), requestCode);
                return true;
            }
        }
        return false;
    }
}

然后将此代码放在需要用户许可的地方.

Then put this code where you require permission from user.

if (!AppPermission.isPermissionRequestRequired(SignInActivity.this, new String[]{"android.permission.GET_ACCOUNTS"},
        REQUEST_APP_PERMISSION)) {
    // Your code if permission available
}

在此之后,在您的 FragmentActivity 中放入此代码 -

After this, in your Fragment or Activity put this code -

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST_APP_PERMISSION:
            for (int i = 0; i < permissions.length; i++) {
                String permission = permissions[i];
                int grantResult = grantResults[i];
                switch (permission) {
                    case "android.permission.GET_ACCOUNTS":
                        if (PackageManager.PERMISSION_GRANTED == grantResult) {
                            // Your code
                        }
                        break;
                }
            }
            break;
    }
}

以上代码用于请求GET_ACCOUNTS的权限,您可以将其更改为所需的任何内容.

The above code is for request permission for GET_ACCOUNTS you can change it to whatever required.

这篇关于Android Marshmallow:在运行时更改权限会导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆