SurfingViolinist
SurfingViolinist

Reputation: 51

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. When I click my Login button, I check to see if the user has gps permissions. 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.

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. 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);
    }
}

Upvotes: 0

Views: 676

Answers (2)

SurfingViolinist
SurfingViolinist

Reputation: 51

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. Unfortunately, Android kept executing with out crashing .. it just did nothing. I simply added a setter to the object and set the value to 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.

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

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

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 (requestCode == 123 && grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

Upvotes: 1

Related Questions