Search code examples
androidandroid-permissions

What is the use of the function : setRequestPermisssionsResult?


This was a sample code given by my lecturer. I understand the whole thing except for the part where there is a function called "setRequestPermisssionsResult". I don't see it called anywhere in the code and I am wondering whether it is of any use.

The program runs perfectly.

    }
    public void EnableRuntimePermission() {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INTERNET}, REQUEST_CODE);
    }

    public void setRequestPermisssionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    Toast.makeText(MainActivity.this, "Internet permission granted.", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "Internet permission not granted.", Toast.LENGTH_LONG).show();
                }

                break;
        }

    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EnableRuntimePermission();
        if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.INTERNET)== PackageManager.PERMISSION_GRANTED){
            Toast.makeText(getApplicationContext(),"Internet Permission Granted", Toast.LENGTH_LONG).show();
        FetchData fetchData=new FetchData();
        fetchData.execute("a18b978603316d47c572d98d52a420f6");
        }

Solution

  • If the method is not called at all then it means it is not being used.

    Anyways, there are 2 ways of requesting permission in Android nowadays. One from the Android Manifest (which doesn't directly ask the user) and programmatically from Java/Kotlin code. These are "more important ones" .In this case the user will see a dialog for accessing something on his phone, maybe camera hardware or something else, and he has the right to allow or deny. Your professor has just generified a method for all permissions to be stored there, which means he as refactored the way of asking for permissions all over the app.

    Here is the documentation for permission