美文网首页
安卓6.0权限适配

安卓6.0权限适配

作者: 剃头匠搬砖 | 来源:发表于2017-02-27 13:43 被阅读0次

    使用三方框架

    compile 'com.lovedise:permissiongen:0.0.6

    //1.请求权限

    btn.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

    PermissionGen.with(MainActivity.this)

    .addRequestCode(100)

    .permissions(

    Manifest.permission.READ_CONTACTS,

    Manifest.permission.RECEIVE_SMS,

    Manifest.permission.WRITE_CONTACTS)

    .request();

    }

    });

    /**

    * 授权成功,后处理的逻辑

    */

    @PermissionSuccess(requestCode = 100)

    public void doSomething() {

    Toast.makeText(this, "Contact permission is granted", Toast.LENGTH_SHORT).show();

    }

    /**

    * 授权失败,后处理的逻辑

    */

    @PermissionFail(requestCode = 100)

    public void doFailSomething() {

    Toast.makeText(this, "Contact permission is not granted", Toast.LENGTH_SHORT).show();

    PermissionGen.with(MainActivity.this)

    .addRequestCode(100)

    .permissions(

    Manifest.permission.READ_CONTACTS,

    Manifest.permission.RECEIVE_SMS,

    Manifest.permission.WRITE_CONTACTS)

    .request();

    }

    @Override

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    PermissionGen.onRequestPermissionsResult(MainActivity.this, requestCode, permissions, grantResults);

    }

    需要在清单文件中先声明好对应的权限,READ_CONTACTS,RECEIVE_SMS,WRITE_CONTACTS

    相关文章

      网友评论

          本文标题:安卓6.0权限适配

          本文链接:https://www.haomeiwen.com/subject/wxszwttx.html