Github

Github 好用分享PermissionX (訪問權限)

1.先導入函示庫(build.gradle: Module)

dependencies {
    implementation 'com.permissionx.guolindev:permissionx:1.4.0'
}

2.添加要的權限到AndroidManifest

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

3.用法解析

初始化
PermissionX.init(this)
取得權限列表
permissions(
    Manifest.permission.CAMERA,
    Manifest.permission.READ_CONTACTS,
    Manifest.permission.CALL_PHONE
)
取得未取得的權限
//先跳出原因
.explainReasonBeforeRequest()

.onExplainRequestReason { deniedList ->
    showRequestReasonDialog(deniedList, "要跟妳拿權限", "確定", "取消")
}
一直拒絕的Alert直接跳去APP設定
.onForwardToSettings { deniedList ->
    showForwardToSettingsDialog(deniedList, "妳要自己去開,因為妳一直拒絕", "好喔", "不要")
}
權限回傳
//先跳出原因
.request { allGranted, grantedList, deniedList ->
    if (allGranted) {
        Toast.makeText(this, "權限都拿到了 3Q", Toast.LENGTH_SHORT).show()
    } else {
        Toast.makeText(this, "妳拒絕了這些權限:$deniedList", Toast.LENGTH_SHORT).show()
    }
}

3.程式碼範例

PermissionX.init(this)
    .permissions(
        Manifest.permission.CAMERA,
        Manifest.permission.READ_CONTACTS,
        Manifest.permission.CALL_PHONE
    )
    .explainReasonBeforeRequest()
    .onExplainRequestReason { deniedList ->
        showRequestReasonDialog(deniedList, "要跟妳拿權限", "確定", "取消")
    }
    .onForwardToSettings { deniedList ->
        showForwardToSettingsDialog(deniedList, "妳要自己去開,因為妳一直拒絕", "好喔", "不要")
    }
    .request { allGranted, grantedList, deniedList ->
        if (allGranted) {
            Toast.makeText(this, "權限都拿到了 3Q", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this, "妳拒絕了這些權限:$deniedList", Toast.LENGTH_SHORT).show()
        }
    }

4.效果展示

作者的Github

PermissionX

發表迴響