Hanbit the Developer

Check If Permissions is granted or denied And Request Permissions in Android 본문

Android

Check If Permissions is granted or denied And Request Permissions in Android

hanbikan 2021. 5. 20. 21:56

여러 개의 권한을 묶어서 권한이 허용되었는지 확인하고, 개중 허용되지 않은 권한들에 대해서 권한 요청을 보내는 상황이다.

 

 - 권한 획득 여부 확인

ContextCompat.checkSelfPermission(Context context, String permission)는 PERMISSION_GRANTED 또는 PERMISSION_DENIED를 반환한다.

 

 - 권한 요청

ActivityCompat.requestPermissions(Activity activity, String[] permissions, int requestCode)는 Array로 묶인 권한들을 전부 요청해준다. 여기서 requestCode는 단순히, 요청을 하는 액티비티를 구분하기 위한 코드이다.

 

 - import

import android.Manifest
import androidx.core.content.ContextCompat
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat

 

 - Example In Fragment

class Fragment1 : Fragment() {
    private val multiplePermissionsCode = 100
    private val requiredPermissions = arrayOf(
        Manifest.permission.INTERNET,
        Manifest.permission.USE_FULL_SCREEN_INTENT,
        Manifest.permission.FOREGROUND_SERVICE,
        Manifest.permission.RECEIVE_BOOT_COMPLETED
    )
    
	override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val deniedPermissions = getDeniedPermissions()
        requestPermissions(deniedPermissions)
    }
}

private fun getDeniedPermissions():ArrayList<String>{
        var deniedPermissions = ArrayList<String>()

        for(p in requiredPermissions){
            if(ContextCompat.checkSelfPermission(requireContext(), p) == PackageManager.PERMISSION_DENIED){
                deniedPermissions.add(p)
            }
        }

        return deniedPermissions
    }
 
 private fun requestPermissions(permissions: ArrayList<String>){
        if(permissions.isNotEmpty()){
            val array = arrayOfNulls<String>(permissions.size)
            ActivityCompat.requestPermissions(requireActivity(), permissions.toArray(array), REQUEST_CODE)
        }
    }

 

 - Example in Activity

class MainActivity : AppCompatActivity() {
    private val multiplePermissionsCode = 100
    private val requiredPermissions = arrayOf(
        Manifest.permission.INTERNET,
        Manifest.permission.USE_FULL_SCREEN_INTENT,
        Manifest.permission.FOREGROUND_SERVICE,
        Manifest.permission.RECEIVE_BOOT_COMPLETED
    )
    
	override fun onCreate(savedInstanceState: Bundle?) {
    
        // 지도 권한
        val deniedPermissions = getDeniedPermissions()
        requestPermissions(deniedPermissions)
    }
}

private fun getDeniedPermissions():ArrayList<String>{
        var deniedPermissions = ArrayList<String>()

        for(p in requiredPermissions){
            if(ContextCompat.checkSelfPermission(this, p) == PackageManager.PERMISSION_DENIED){
                deniedPermissions.add(p)
            }
        }

        return deniedPermissions
    }
 
 private fun requestPermissions(permissions: ArrayList<String>){
        if(permissions.isNotEmpty()){
            val array = arrayOfNulls<String>(permissions.size)
            ActivityCompat.requestPermissions(this, permissions.toArray(array), REQUEST_CODE)
        }
    }

 

 

https://developer.android.com/training/permissions/requesting?hl=ko