How to use EasyPermissions

Starting with Android 6.0 (API level 23), users begin to grant permissions to applications when they are running, rather than when they are installed.

System authority can be divided into two types: normal authority and dangerous authority
-Normal permissions do not directly bring risks to users' privacy. If your app lists a normal permission in its list, it will be granted automatically.
-Dangerous permissions grant the app access to the user's confidential data. If your app lists a normal permission in its list, it will be granted automatically. If you list dangerous permissions, users must explicitly approve your app to use them.

1.Google's original method of requesting permission

   //Normal access
    private void checkPermissionForNormal() {
        //Determine whether to agree to this authority
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {           
                //Application authority
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 100);
        } else {
            Toast.makeText(this, "Authorized CALL_PHONE", Toast.LENGTH_LONG).show();

Callback of permission

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 100: {
                //If the returned result array is greater than 0, there is a result
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();

2. Encapsulated tripartite Library - EasyPermissions
This is a very simple application for permission

  • Import into storage
compile 'pub.devrel:easypermissions:0.1.5'  
  • 1
  • Code block
  • package com.example.easypermissions_demo;
    import android.Manifest;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    import java.util.List;
    import pub.devrel.easypermissions.EasyPermissions;
    public class MainActivity extends AppCompatActivity implements View.OnClickListener,EasyPermissions.PermissionCallbacks {
        private static final String TAG = "MainActivity";
        private static final int CAMERA_REQUESTCODE = 101;
        private static final int LOCATION_CONTACTS_REQUESTCODE = 102;
        private Button button_camera;
        private Button button_location_and_contacts;
        protected void onCreate(Bundle savedInstanceState) {
            button_camera = (Button) findViewById(;
            button_location_and_contacts = (Button) findViewById(;
        public void onClick(View v) {
            switch (v.getId()) {
        public void requestCamera() {
            if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
                Toast.makeText(this, "Authorized Camera", Toast.LENGTH_LONG).show();
            } else {
                // request for one permission
                EasyPermissions.requestPermissions(this, getString(R.string.rationale_camera),
                        CAMERA_REQUESTCODE, Manifest.permission.CAMERA);
        public void requestLocationAndContacts() {
            String[] perms = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS };
            if (EasyPermissions.hasPermissions(this, perms)) {
                Toast.makeText(this, "Authorized contact and location information", Toast.LENGTH_LONG).show();
            } else {
                // request for both permissions
                EasyPermissions.requestPermissions(this, getString(R.string.rationale_location_contacts),
                        LOCATION_CONTACTS_REQUESTCODE, perms);
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            // EasyPermissions permission processing request result
            Log.i(TAG,"onRequestPermissionsResult:"+ requestCode);
            EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
        //Agree to authorize
        public void onPermissionsGranted(int requestCode, List<String> perms) {
            Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size());
        //Deny authorization
        public void onPermissionsDenied(int requestCode, List<String> perms) {
            Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());

Original address:


Tags: Android Google Permission denied Java

Posted on Sat, 04 Jan 2020 07:05:47 -0500 by blanius