Search code examples
androidandroid-permissions

How to check permission and request permission in (Easy coding & easy to understand)


I search for Run-Time-Permission in android and I find lots of links but I see there is complex code.

Note: I share my simple code here so it's helpful for a new developer or Fraser. Also, I didn't use any third party library here.


Solution

  • First of all, Write permission in Manifest file(I take some bagic permissions):

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    Now I make two methods checkPermission() AND requestPermission() also @Override one method name is onRequestPermissionsResult() this is take care of result.

    Now I'll show you how it looks like:

    private boolean checkPermission() {
            int internetPermission = ContextCompat.checkSelfPermission(getApplicationContext(), INTERNET);
            int cameraPermission = ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA);
            int readStoragePermission = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
            int writeStoragePermission = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
    
            return internetPermission == PackageManager.PERMISSION_GRANTED &&
                    cameraPermission == PackageManager.PERMISSION_GRANTED &&
                    readStoragePermission == PackageManager.PERMISSION_GRANTED &&
                    writeStoragePermission == PackageManager.PERMISSION_GRANTED;
        }
    

    You see in above code there is define INTERNET, CAMERA etc.. This is an import by clicking Alt + Enter And it is look like see below code:

    import static android.Manifest.permission.CAMERA;
    import static android.Manifest.permission.INTERNET;
    import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
    import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
    

    Now Please see below code of requestPermission() Method :

    private void requestPermission() {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{INTERNET, CAMERA, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, 1);
        }
    

    Now the Last method is onRequestPermissionsResult(). In this method you can see either permission is GRANTED or DENY:

    @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    
            switch (requestCode) {
    
                case 1:
                    if (grantResults.length > 0) {
                        boolean internet = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                        boolean camera = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                        boolean readStorage = grantResults[2] == PackageManager.PERMISSION_GRANTED;
                        boolean writeStorage = grantResults[3] == PackageManager.PERMISSION_GRANTED;
    
                        if (internet && camera && readStorage && writeStorage) {
                            init();
                           // Permission GRANTED (Here write your code what you want).
                        } else {
                            // Permission DENY (If user click on DENY then finish the activity);
                            finish();
                        }
                    }
            }
        }
    

    Great you are done with checkPermission & requestPermission.

    NOTE:

    This above code is working for Activity. There is a minor change in the fragment.

    If I use this code in fragment then what should I change:

    You want to just change in requestPermission() Method:

    private void requestPermission() {
                requestPermissions(new String[]{INTERNET, CAMERA, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, 1);
            }