Giter VIP home page Giter VIP logo

easypermissions's Introduction

EasyPermissions

一个及其简洁的安卓runtime权限库
特点如下:

  • 链式操作
  • 若请求的权限未在manifest中注册,将抛出明确的异常
  • 自动重试(可配置),配置该选项后若请求被拒但用户未勾选不再提示框时会自动重试直到用户授权或勾选不再提示框

Gradle依赖

1.在项目根build.gradle中添加

allprojects {
	repositories {
		maven { url 'https://jitpack.io' }
	}
}

2.在需要依赖本库的build.gradle中添加

dependencies {
    implementation 'com.github.Ficat:EasyPermissions:v2.1.0'
}

使用

//requestEach方式
EasyPermissions
       .with(activity)
       .requestEach(Manifest.permission.CAMERA)
       .result(new RequestEachExecutor.ResultReceiver() {
           @Override
           public void onPermissionsRequestResult(Permission permission) {
               String name = permission.name;
               if (permission.granted) {
                   //name权限被授予
               } else {
                   if (permission.shouldShowRequestPermissionRationale) {
                       //name权限被拒绝但用户未勾选不再提示框,可继续请求
                   } else {
                       //name权限被拒绝且用户勾选了不再提示框
                       //此时不能再次请求了,而需要user前往设置界面手动授权
                       EasyPermissions.goToSettingsActivity(activity);
                   }
               }
           }
       });


//request方式,请求的所有权限被用户授权后返回true,否则返回false  
EasyPermissions
        .with(activity)
        .request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        .autoRetryWhenUserRefuse(true, new BaseRequestExecutor.RequestAgainListener() {//是否自动重试
            @Override
            public void requestAgain(String[] needAndCanRequestAgainPermissions) {
                //该监听回调中传入的是再次请求的权限,用以在重新请求时弹出说明框等信息(如
                //向用户说明为何要使用该权限)
                for (String s : needAndCanRequestAgainPermissions) {
                    Log.e("TAG", "request again permission = "+s);
                }
            }
        })
        .result(new RequestExecutor.ResultReceiver() {
            @Override
            public void onPermissionsRequestResult(boolean grantAll, List<Permission> results) {
                if (grantAll) {
                    Toast.makeText(MainActivity.this, "request permissions success!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "request permissions fail!", Toast.LENGTH_SHORT).show();
                }
            }
        });

致谢

RxPermissions

License

Copyright 2018 Ficat

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.