運(yùn)行時(shí)權(quán)限標(biāo)準(zhǔn)化實(shí)例

聲明權(quán)限
<uses-permission android:name="android.permission.CAMERA"/>
注冊(cè)啟動(dòng)器
//注冊(cè)權(quán)限回調(diào),它處理用戶對(duì)系統(tǒng)權(quán)限對(duì)話框的響應(yīng)。
//保存返回值,ActivityResultLauncher的一個(gè)實(shí)例。你可以使用val,
//如這段代碼所示,也可以在onAttach()或onCreate()方法中使用lateinit var。
val requestPermissionLauncher =
    registerForActivityResult(RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            // 授予許可。繼續(xù)你的應(yīng)用程序中的動(dòng)作或工作流程
        } else {
           //向用戶解釋該特性不可用,因?yàn)樵撎匦孕枰脩艟芙^的權(quán)限。
           //同時(shí),尊重用戶的決定。不要為了說服用戶改變他們的決定而鏈接系統(tǒng)設(shè)置。
        }
    }
向用戶請(qǐng)求權(quán)限的建議流程
when {
    ContextCompat.checkSelfPermission(
            CONTEXT,
            Manifest.permission.REQUESTED_PERMISSION
            ) == PackageManager.PERMISSION_GRANTED -> {
        // 您可以使用需要該權(quán)限的API。
    }
    shouldShowRequestPermissionRationale(...) -> {
        //在教育性UI中,向用戶解釋為什么你的應(yīng)用程序需要這個(gè)權(quán)限才能讓某個(gè)特性按照預(yù)期的方式運(yùn)行。
        //在這個(gè)UI中,包含一個(gè)“取消”或“不感謝”按鈕,允許用戶在沒有授權(quán)的情況下繼續(xù)使用你的應(yīng)用。
        showInContextUI(...)
    }
    else -> {
        //你可以直接請(qǐng)求許可,注冊(cè)的ActivityResultCallback獲取此請(qǐng)求的結(jié)果。
        requestPermissionLauncher.launch(
                Manifest.permission.REQUESTED_PERMISSION)
    }
}
自行管理請(qǐng)求的實(shí)例
when {
    ContextCompat.checkSelfPermission(
            CONTEXT,
            Manifest.permission.REQUESTED_PERMISSION
            ) == PackageManager.PERMISSION_GRANTED -> {
        // You can use the API that requires the permission.
        performAction(...)
    }
    shouldShowRequestPermissionRationale(...) -> {
        // In an educational UI, explain to the user why your app requires this
        // permission for a specific feature to behave as expected. In this UI,
        // include a "cancel" or "no thanks" button that allows the user to
        // continue using your app without granting the permission.
        showInContextUI(...)
    }
    else -> {
        // You can directly ask for the permission.
        requestPermissions(CONTEXT,
                arrayOf(Manifest.permission.REQUESTED_PERMISSION),
                REQUEST_CODE)
    }
}
override fun onRequestPermissionsResult(requestCode: Int,
        permissions: Array<String>, grantResults: IntArray) {
    when (requestCode) {
        PERMISSION_REQUEST_CODE -> {
            // If request is cancelled, the result arrays are empty.
            if ((grantResults.isNotEmpty() &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                // Permission is granted. Continue the action or workflow
                // in your app.
            } else {
                // Explain to the user that the feature is unavailable because
                // the features requires a permission that the user has denied.
                // At the same time, respect the user's decision. Don't link to
                // system settings in an effort to convince the user to change
                // their decision.
            }
            return
        }

        // Add other 'when' lines to check for other
        // permissions this app might request.
        else -> {
            // Ignore all other requests.
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容