GithubHelp home page GithubHelp logo

sendtion / andpermission Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yanzhenjie/andpermission

0.0 2.0 0.0 104 KB

Android permission.

Home Page: http://blog.csdn.net/yanzhenjie1003/article/details/52503533

Java 100.00%

andpermission's Introduction

AndPermission

严振杰的主页:http://www.yanzhenjie.com
严振杰的博客:http://blog.yanzhenjie.com
技术交流群:547839514,加群前请务必阅读群行为规范

更多解释请看Android6.0 运行时权限管理最佳实践详解


引用方法

  • AndroidStudio使用方法,gradle一句话远程依赖
compile 'com.yanzhenjie:permission:1.0.0'

Or Maven:

<dependency>
  <groupId>com.yanzhenjie</groupId>
  <artifactId>permission</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>

我在开发SwipeRecyclerView时引用的RecyclerView版本如下:

compile 'com.android.support:recyclerview-v7:23.4.0'

使用介绍

更好的例子,请下载源码后运行demo查看,这里给出最关键的代码。

1、申请权限就是这么简单

AndPermission.with(this)
    .requestCode(101)
    .permission(Manifest.permission.WRITE_CONTACTS,
        Manifest.permission.READ_SMS,
        Manifest.permission.WRITE_EXTERNAL_STORAGE)
    .send();

只需要在Activity中或者Fragment中直接调用即可,AndPermission自动为你打理好后宫。

2、接受权限回调更简单 只需要重写Activity/Fragment的一个方法,然后提供一个授权时回调的方法即可:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    // 只需要调用这一句,剩下的AndPermission自动完成。
    AndPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}

// 成功回调的方法,用注解即可,里面的数字是请求时的requestCode。
@PermissionYes(100)
private void getLocationYes() {
    // 申请权限成功,可以去做点什么了。
    Toast.makeText(this, "获取定位权限成功", Toast.LENGTH_SHORT).show();
}

// 失败回调的方法,用注解即可,里面的数字是请求时的requestCode。
@PermissionNo(100)
private void getLocationNo() {
    // 申请权限失败,可以提醒一下用户。
    Toast.makeText(this, "获取定位权限失败", Toast.LENGTH_SHORT).show();
}

只需要上面这么几句话即可,你就可以大刀阔斧的干了,在总结中提到的各种判断、复杂的情况AndPermission自动完成。

3、如果你需要在用户多次拒绝权限后提示用户

AndPermission.with(this)
    .requestCode(101)
    .permission(Manifest.permission.WRITE_CONTACTS,
        Manifest.permission.READ_SMS,
        Manifest.permission.WRITE_EXTERNAL_STORAGE)
    .rationale(mRationaleListener)
    .send();

private RationaleListener mRationaleListener = new RationaleListener() {
    @Override
    public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
        new AlertDialog.Builder(RationalePermissionActivity.this)
            .setTitle("友好提醒")
            .setMessage("没有定位权限将不能为您推荐附近妹子,请把定位权限赐给我吧!")
            .setPositiveButton("好,给你", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    rationale.resume();// 用户同意继续申请。
                }
            })
            .setNegativeButton("我拒绝", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    rationale.cancel(); // 用户拒绝申请。
                }
        }).show();
    }
};

License

Copyright 2016 Yan Zhenjie

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.

andpermission's People

Watchers

 avatar  avatar

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.