Android 6.0 multiple permissions

后端 未结 22 2220
太阳男子
太阳男子 2020-11-22 03:58

I know that Android 6.0 has new permissions and I know I can call them with something like this

if (ContextCompat.checkSelfPermission(this, Manifest.permiss         


        
22条回答
  •  轮回少年
    2020-11-22 04:17

    I just use an array to achieved multiple requests, hope it helps someone. (Kotlin)

        // got all permission
        private fun requestPermission(){
            var mIndex: Int = -1
            var requestList: Array = Array(10, { "" } )
    
            // phone call Permission
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                mIndex ++
                requestList[mIndex] = Manifest.permission.CALL_PHONE
            }
            // SMS Permission
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                mIndex ++
                requestList[mIndex] = Manifest.permission.SEND_SMS
            }
    
            // Access photos Permission
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                mIndex ++
                requestList[mIndex] = Manifest.permission.READ_EXTERNAL_STORAGE
            }
    
            // Location Permission
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                mIndex ++
                requestList[mIndex] = Manifest.permission.ACCESS_FINE_LOCATION
            }
    
            if(mIndex != -1){
                ActivityCompat.requestPermissions(this, requestList, PERMISSIONS_REQUEST_ALL)
            }
        }
    
    
        // permission response
        override fun onRequestPermissionsResult(requestCode: Int,
                                                permissions: Array, grantResults: IntArray) {
            when (requestCode) {
                PERMISSIONS_REQUEST_ALL -> {
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        // permission accept location
                        if (ContextCompat.checkSelfPermission(this,
                                        Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                            Log.d(TAG, "Phone Call permission accept.")
                        }
    
                        // permission accept location
                        if (ContextCompat.checkSelfPermission(this,
                                        Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
                            Log.d(TAG, "SMS permission accept.")
                        }
    
                        // permission accept location
                        if (ContextCompat.checkSelfPermission(this,
                                        Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                            Log.d(TAG, "SMS permission accept.")
                        }
    
                        // permission accept location
                        if (ContextCompat.checkSelfPermission(this,
                                        Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                            Log.d(TAG, "Location permission accept.")
                        }
    
                    } else {
                        Toast.makeText(mContext, "Permission Failed!", Toast.LENGTH_LONG).show()
                    }
                    return
                }
            }
        }
    

提交回复
热议问题