美文网首页
android 6.0动态权限

android 6.0动态权限

作者: 小龙9000 | 来源:发表于2018-08-29 10:24 被阅读13次

    In Activity class:

    
    
        private fun initPermission() {
             val permissions = arrayOf(Manifest.permission.INTERNET, 
             Manifest.permission.ACCESS_NETWORK_STATE, 
             Manifest.permission.MODIFY_AUDIO_SETTINGS, 
             Manifest.permission.WRITE_EXTERNAL_STORAGE, 
             Manifest.permission.WRITE_SETTINGS, 
             Manifest.permission.READ_PHONE_STATE, 
             Manifest.permission.ACCESS_WIFI_STATE, 
             Manifest.permission.CHANGE_WIFI_STATE)
    
            val toApplyList = ArrayList<String>()
    
            for (perm in permissions) {
                if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
                    toApplyList.add(perm)
                    //进入到这里代表没有权限.
                }
            }
            
            val tmpList = arrayOfNulls<String>(toApplyList.size)
            if (!toApplyList.isEmpty()) {
                ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123)// three times to ask
            }
    
        }
    
    
        override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
            // 此处为android 6.0以上动态授权的回调
        }
    
    

    In onCreate:

      initPermission()
    

    相关文章

      网友评论

          本文标题:android 6.0动态权限

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