Android development runtime permission dynamically applied tool class (extension supported)

Keywords: Android SDK Mobile

Android 6.0 or above (API > = 23), or build.version.sdk ﹐ int > = build.version ﹐ codes. M, sensitive permissions like camera, location, mobile phone address book, phone call and other permissions need dynamic application, first attach the flow chart, for reference only, you can carry out self-defined secondary encapsulation extension according to this process:

Flow chart of dynamic application authority:

Tool class of dynamic permission

package com.refly.pigcommissioner.permissionutil;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 *
 * @author ytf
 * @date 2018/4/25
 */

public class PermissionUtils {
    private Activity context;
    public static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 200;

    private static PermissionUtils instance = null;

    private PermissionUtils(Activity context) {
        this.context = context;
    }

    /**
     * Single object instance
     */
    public static PermissionUtils getInstance(Activity context) {
        if (instance == null) {
            instance = new PermissionUtils(context);
        }
        return instance;
    }

    /**
     * Whether dynamic application permission is required
     *
     * @param requestCode
     */
    public boolean needPermission(int requestCode) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return false;
        }else {
            return  true;
        }
//        requestAllPermissions(requestCode);
    }

    /**
     * Common all sensitive permissions
     * @param requestCode
     */
    public void requestAllPermissions(int requestCode) {
        ActivityCompat.requestPermissions(context,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_CALL_PHONE);
    }
    /**
     * Permission to make a call
     * @param requestCode
     */
    public boolean requesCallPhonePermissions(int requestCode) {
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {//No authority
            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
            return false;
        } else {
            return true;
        }
    }
    /**
     * Read access to memory card
     * @param requestCode
     */
    public boolean requestReadSDCardPermissions(int requestCode) {
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {//No authority
            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
            return false;
        } else {
            return true;
        }
    }
    /**
     * Get camera permission
     * @param requestCode
     */
    public boolean requestCamerPermissions(int requestCode) {
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {//No authority
            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
            return false;
        } else {
            return true;
        }
    }
    /**
     * Access to mobile contacts
     * @param requestCode
     */
    public boolean requestReadConstantPermissions(int requestCode) {
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {//No authority
            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.READ_CONTACTS},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
            return false;
        } else {
            return true;
        }
    }
    /**
     * Get user rights
     * @param requestCode
     */
    public boolean requestGET_ACCOUNTSPermissions(int requestCode) {
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.GET_ACCOUNTS)
                != PackageManager.PERMISSION_GRANTED) {//No authority
            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.GET_ACCOUNTS},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
            return false;
        } else {
            return true;
        }
    }
    /**
     * Get location permission
     * @param requestCode
     */
    public boolean requestLocationPermissions(int requestCode) {
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {//No authority
            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
            return false;
        } else {
            return true;
        }
    }

}

Core code used by tool class:

   private com.refly.pigcommissioner.permissionutil.PermissionUtils instance = null;

    @AfterViews
    void afterViews() {
   . . . 
        //Determine whether it is Android 6.0System version, if yes, need to add permissions dynamically
      instance=com.refly.pigcommissioner.permissionutil.PermissionUtils.getInstance(this);
        if (instance.needPermission(com.refly.pigcommissioner.permissionutil.PermissionUtils.MY_PERMISSIONS_REQUEST_CALL_PHONE)){
//           Return true to indicate dynamic permission application is required
            instance.requestLocationPermissions(200);
        }else {
//            Return false without dynamic application, direct operation
        }
. . . . 

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            // requestCode is the declared permission acquisition code, which is passed in when checkSelfPermission
            case BAIDU_READ_PHONE_STATE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Obtain the permission and handle it accordingly (when calling the location SDK, make sure that all relevant permissions are authorized, otherwise the location may fail)
                    locationService = ((MainApp) getApplication()).locationService;
                    locationService.registerListener(myListener);
                    locationService.start();
                } else {
                    // No permission obtained, special handling
                    Toast.makeText(getApplicationContext(), "Failed to get location permission, please turn it on manually", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                Logger.e("Failed to get location permission, please turn it on manually");
                break;
        }
    }

Posted by landysaccount on Mon, 30 Mar 2020 09:44:14 -0700