Use external class to request permissions on android

0

I am implementing an external class in my android project to manage permissions:

class RequesterPermissions extends ActivityCompat {

    private final static String TAG = "RequesterPermissions";
    private Activity activity;

    RequesterPermissions(Activity activity){
        this.activity = activity;
    }

    boolean checkIfPermissionIsGranted(String permission){
        boolean Return = false;
        int permissionChecked = ContextCompat.checkSelfPermission(this.activity, permission);
        if(permissionChecked == PackageManager.PERMISSION_GRANTED){
            // Permission already granted
            Return = true;
        }else if(permissionChecked == PackageManager.PERMISSION_DENIED){
            // Permission denied
            Return = false;
        }
        return Return;
    }

    void requestForPermission(String[] permission){
        // Open dialog to grant or denied permission
        ActivityCompat.requestPermissions(this.activity, permission, 1);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission granted
                    Log.d(TAG, permissions[0] + " granted");
                } else {
                    // Permission denied
                    Log.e(TAG, permissions[0] + " denied");
                }
            }
        }
    }
}

My problem is that I throw this error in the onRequestPermissionsResult() method:

  

Error: (41, 5) error: method does not override or implement a method from a supertype

How can I solve this?

    
asked by Jorius 22.11.2016 в 01:12
source

3 answers

1

I solved my problem by changing the start of my class in the following way:

class RequesterPermissions extends ActivityCompat {

By:

class RequesterPermissions implements ActivityCompat.OnRequestPermissionsResultCallback {
    
answered by 22.11.2016 / 01:20
source
0

I see that you solved your problem, however, because of the error that you understand, I still need to add the invocation to super within the onRequestPermissionsResult method as follows:

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

    ...
}

With your solution the problem no longer exists because you no longer inherit from a class (there is no 'super') but you implement an interface which may not be ideal depending on what you want to do.

    
answered by 23.11.2016 в 03:01
0

I'm implementing part of your code but I do not get into

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

as you do to capture the answer

    
answered by 13.08.2018 в 19:56