An overview of the

With the popularity of Android6.0, permission application has become a mandatory part of our development code. For example, SD card permission, location permission, and photo permission are almost necessary for every app. Normally, we apply for permission before we need it, and we make all kinds of judgments. Since there are some necessary permissions, why don’t we apply for all of them at the first time we open the app? But the application of so many permissions is obviously not very friendly to the user, so we might as well tell the user before applying permissions, so that the user has a psychological preparation. Therefore, it uses the permission application interface of Ele. me for reference and encapsulates a library

The effect





rendering

The project address

Github.com/yewei02538/…

process

In the process of applying for permission will inevitably be rejected by the user ruthlessly, then will pop up to tell the user that this is a must, you must agree! If the user chooses to refuse again, then we will direct the user to open our permissions in permissions Management. If the user still refuses, then.. I’m at my fucking wit’s end!!

use

Gradle:

compile 'me.weyye.hipermission:library:1.01.'Copy the code

Or Maven:

<dependency>
  <groupId>me.weyye.hipermission</groupId>
  <artifactId>library</artifactId>
  <version>1.0.1</version>
  <type>pom</type>
</dependency>Copy the code

One line of code

HiPermission.create(context)
    .checkMutiPermission(new PermissionCallback() {
        @Override
        public void onClose(a) {
            Log.i(TAG, "onClose");
            showToast("User closes permission request");
        }

        @Override
        public void onFinish(a) {
            showToast("All permissions requested completed.");
        }

        @Override
        public void onDeny(String permisson, int position) {
            Log.i(TAG, "onDeny");
        }

        @Override
        public void onGuarantee(String permisson, int position) {
            Log.i(TAG, "onGuarantee"); }});Copy the code

It’s easy to get the three necessary permissions

Do you want to apply for other privileges? That’s fine

List<PermissonItem> permissonItems = new ArrayList<PermissonItem>();
permissonItems.add(new PermissonItem(Manifest.permission.CAMERA, "Camera", R.drawable.permission_ic_memory));
permissonItems.add(new PermissonItem(Manifest.permission.ACCESS_FINE_LOCATION, "Positioning", R.drawable.permission_ic_location));
HiPermission.create(MainActivity.this) .permissions(permissonItems) .checkMutiPermission(...) ;Copy the code

What? Want to change the message? Does the interface not match your theme color? so easy

HiPermission.create(MainActivity.this)
            .title("Dear God.")
            .permissions(permissonItems)
            .filterColor(ResourcesCompat.getColor(getResources(), R.color.colorPrimary, getTheme()))// The color of the icon
            .msg("To protect the peace of the world, open these permissions! \n Together you and I save the world!") .style(R.style.PermissionBlueStyle) .checkMutiPermission(...) ;Copy the code

Be sure to call filterColor() after setting the theme, otherwise the permissions icon will change to black by default

styles.xml

    <style name="PermissionBlueStyle">
        <item name="PermissionTitleColor">@color/colorPrimaryDark</item>
        <item name="PermissionMsgColor">@color/colorPrimary</item>
        <item name="PermissionItemTextColor">@color/colorPrimary</item>
        <item name="PermissionButtonBackground">@drawable/shape_btn</item>
        <item name="PermissionBackround">@drawable/shape_bg_white</item>
        <item name="PermissionButtonTextColor">@android:color/white</item>
    </style>Copy the code




rendering

The following is an explanation of each attribute

The property name type explain
PermissionTitleColor int Title text color
PermissionMsgColor int Description text color
PermissionItemTextColor int Permission text color
PermissionButtonTextColor int Button text color
PermissionButtonBackground drawable Button background
PermissionButtonTextColor drawable Dialog box background

The last

If it works for you, why not get a star?