简体   繁体   English

Android onRequestPermissionsResult无法正常工作

[英]Android onRequestPermissionsResult not working correctly

I have a weird bug that is driving me crazy. 我有一个奇怪的错误,使我发疯。 I'm working on Android Marshmallow and I'm implementing the new permissions. 我正在开发Android Marshmallow,并且正在实现新权限。 When I click my Login button, I check to see if the user has gps permissions. 当我单击“登录”按钮时,我检查用户是否具有gps权限。 If not, I request them. 如果没有,我要求他们。 The way the code works is that after permissions are asked, an Async task is called to get some settings from a REST service, then on the onPostExecute of the Async task, it will fire an Intent. 代码的工作方式是:在请求权限后,调用Async任务以从REST服务获取一些设置,然后在Async任务的onPostExecute上将触发一个Intent。

When the user Allows permissions, everything works fine. 当用户允许权限时,一切正常。 If the user denies permissions, it will call the Async task and call the Intent, but it will not activate the Intent. 如果用户拒绝权限,它将调用异步任务并调用Intent,但不会激活Intent。 It simply stays on the screen. 它只是停留在屏幕上。

The Button Click 按钮点击

    Button btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            int has_permission = 0;

            if (Build.VERSION.SDK_INT >= 23)
            {
                has_permission = Check_Permission(Manifest.permission.ACCESS_FINE_LOCATION);
            }

            action = "login";

            if(has_permission == 0)
            {
                Get_Location();

                load_system_settings_async = new Load_System_Settings_Async();
                load_system_settings_async.execute((Void) null);
            }
        }
    });

Check Permission Code 检查权限代码

   protected int Check_Permission(final String permission)
{
    int has_permission = checkSelfPermission(permission);

    if (has_permission != PackageManager.PERMISSION_GRANTED)
    {
        if (!shouldShowRequestPermissionRationale(permission) && (request_times > 0))
        {
            String title="";

            if(permission.equals(Manifest.permission.ACCESS_FINE_LOCATION))
            {
                title = "You need to allow GPS access";
            }
            else if(permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE))
            {
                title = "You need to allow storage access";
            }
            else if(permission.equals(Manifest.permission.CALL_PHONE))
            {
                title = "You need to allow access phone access";
            }

            Show_Alert_Dialog("Ok", title,
                    new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                                    Uri.fromParts("package", getPackageName(), null));
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);

                            //requestPermissions(new String[] {permission}, REQUEST_CODE_ASK_PERMISSIONS);
                        }
                    });
            return has_permission;
        }

        requestPermissions(new String[] {permission}, REQUEST_CODE_ASK_PERMISSIONS);
        request_times++;

        return has_permission;
    }

    return has_permission;
}

Permission Request 权限请求

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    if (requestCode == 123)
    {
        if(grantResults[0] == 0)
        {
            Get_Location();
        }

        load_system_settings_async = new Load_System_Settings_Async();
        load_system_settings_async.execute((Void) null);

        //request_times = 0;
    }
    else
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

it seems like the problem is when user denied the permission ,control fall in onRequestPermissionsResult where you are executing your asynch task even if the permission is not granted .check comment in following code 似乎问题出在用户拒绝权限时,控制权落在onRequestPermissionsResult ,即使没有授予权限,您也可以在其中执行异步任务。

public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
permissions, @NonNull int[] grantResults)
{
    if (requestCode == 123) // yes request code is the same go on 
    {
        if(grantResults[0] == 0) //yes we get the approval  
        {
            Get_Location();
        }
         // oh even if we didn't get the approval we still gonna execute the task
        load_system_settings_async = new Load_System_Settings_Async();
        load_system_settings_async.execute((Void) null);

        //request_times = 0;
    }
    else //control only come here when request code will not match 
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

you need to combine the if condition like this 您需要结合这样的if条件

if (requestCode == 123 && grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

Ok, I figured it out .. When I was passing a custom Object with putExtra in the Intent, there was a property that was null, so when it was executing writeToParcel in the object, it was crashing. 好的,我想出了..当我在Intent中传递带有putExtra的自定义对象时,有一个属性为null,因此当它在对象中执行writeToParcel时,它崩溃了。 Unfortunately, Android kept executing with out crashing .. it just did nothing. 不幸的是,Android继续执行而没有崩溃..它什么也没做。 I simply added a setter to the object and set the value to false. 我只是向对象添加了一个setter,并将值设置为false。 Issue had nothing to do with the Permission code. 问题与权限代码无关。 Four hours of life and sleep lost that I will not get back. 失去了四个小时的生活和睡眠,我再也回不来了。 Thanks all for who viewed. 感谢所有浏览过的人。

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

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