코딩하는 일용직 노동자

Dexter - 퍼미션 라이브러리 본문

안드로이드

Dexter - 퍼미션 라이브러리

bacass 2020. 9. 27. 14:13

안드로이드 퍼미션 라이브러리가 여럿 있습니다. 오늘 소개해드릴 라이브러리는 Dexter입니다.
박상권님이 만드신 TedPermission 만큼이나 사용이 편리합니다.


우선 gradle 에 아래처럼 추가해줍니다.

dependencies{
    implementation 'com.karumi:dexter:6.2.1'
}


Single Permission인 경우 아래처럼 처리합니다.

-Java

Dexter.withContext(this)
	.withPermission(Manifest.permission.CAMERA)
	.withListener(new PermissionListener() {
		@Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */}
		@Override public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
		@Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
	}).check();

-Kotlin

Dexter.withContext(this)
    .withPermission(Manifest.permission.CAMERA)
    .withListener(object : PermissionListener {
        override fun onPermissionGranted(response: PermissionGrantedResponse) {
            /* ... */
        }

        override fun onPermissionDenied(response: PermissionDeniedResponse) {
            /* ... */
        }

        override fun onPermissionRationaleShouldBeShown(
            permission: PermissionRequest?,
            token: PermissionToken?) {
            /* ... */
        }
    }).check()

 


Multi Permission인 경우 아래처럼 처리합니다.

-Java

Dexter.withContext(this)
	.withPermissions(
		Manifest.permission.CAMERA,
		Manifest.permission.READ_CONTACTS,
		Manifest.permission.RECORD_AUDIO
	).withListener(new MultiplePermissionsListener() {
	    @Override public void onPermissionsChecked(MultiplePermissionsReport report) {/* ... */}
	    @Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {/* ... */}
	}).check();

-Kotlin

Dexter.withContext(this)
    .withPermissions(
        Manifest.permission.CAMERA,
        Manifest.permission.READ_CONTACTS,
        Manifest.permission.RECORD_AUDIO
    ).withListener(object : MultiplePermissionsListener {
        override fun onPermissionsChecked(report: MultiplePermissionsReport) {
            /* ... */
        }

        override fun onPermissionRationaleShouldBeShown(
            permissions: List<PermissionRequest>,
            token: PermissionToken
        ) {
            /* ... */
        }
    }).check()

 

더 자세한 사용법은 아래 링크에서 확인할 수 있습니다.    

https://github.com/Karumi/Dexter

 

Karumi/Dexter

Android library that simplifies the process of requesting permissions at runtime. - Karumi/Dexter

github.com