设备管理员添加屏幕未出现

时间:2019-10-29 03:25:04

标签: android kotlin

我正在从Google网站上关注this kotlin example,以提示用户向我的应用授予管理员访问权限,但由于某种原因,该屏幕没有出现。在OnActivityResult中,结果被注册为已取消。我不知道自己在做什么错,但如果有人能指出我正确的方向,我将不胜感激:

    //this code resides in OnCreate method of current activity
    val compName = ComponentName(this, CurrentActivity::class.java)
    val adminActive = devicePolicyManager!!.isAdminActive(compName)

    if (!adminActive) {
        val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)

        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName)
        intent.putExtra(
            DevicePolicyManager.EXTRA_ADD_EXPLANATION,
            "......."
        )
        startActivityForResult(intent, PERMISSION_ADMIN_RIGHT)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode) {
        PERMISSION_ADMIN_RIGHT -> if (resultCode == Activity.RESULT_CANCELED) { // 
            Toast.makeText(
                this@CurrentActivity,
                R.string.WARNING_ADMIN_REQUEST_REFUSE,
                Toast.LENGTH_LONG
            ).show()
        }
    }

    super.onActivityResult(requestCode, resultCode, data)
}

API 28 测试设备:Galaxy S6

0 个答案:

没有答案