How to check permission in Android 6.0 or higher version?

To get read phone call log, you need figure out two-step.


<uses-permission android:name="android.permission.READ_CALL_LOG"/>


Step.1 Check permission you have or not.


//------------------------------------------------

private int REQUEST_CALL_LOG = 1;

private Activity mainActivity = this;

//------------------------------------------------


int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CALL_LOG);


if (permissionCheck == PackageManager.PERMISSION_DENIED) 

{

    // Request permission

    ActivityCompat.requestPermissions(mainActivity, 

                                                  new String[]{Manifest.permission.READ_CALL_LOG},

                                                  REQUEST_CALL_LOG);

else 

{

    // You have permission

}



Step.2 Implement get permission request result.


@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {

        case REQUEST_CALL_LOG:

            for (int i = 0; i < permissions.length; i++) {

                String permission = permissions[i];

                int grantResult = grantResults[i];

                if (permission.equals(Manifest.permission.READ_CALL_LOG)) 

                {

                    if (grantResult == PackageManager.PERMISSION_GRANTED) 

                    {

                        // permission granted

                    }

                } 

           }

     }

}


These two more steps are making annoy to a programmer, but in user side of view, it's good to protect their individual information.