보다 쉽게 도와준다고 함
//tedpermission
implementation 'gun0912.ted:tedpermission:2.2.3'
implementation 'io.github.ParkSangGwon:tedpermission:2.3.0'
둘다 android.support 인건지..^_ㅠ?
gradle.properties 에 추가해주면 됨
android.useAndroidX=true
android.enableJetifier=true
java
private void setPermission(){
Log.d(TAG, "setPermission: ");
PermissionListener permissionListener = new PermissionListener() {
@Override
public void onPermissionGranted() {
// 권한 허용 시
Log.d(TAG, "onPermissionGranted: 권한 허용");
}
@Override
public void onPermissionDenied(List<String> deniedPermissions) {
// 권한 거부시
Log.d(TAG, "onPermissionDenied: 권한 거부");
}
};
TedPermission.with(this)
.setPermissionListener(permissionListener)
.setRationaleMessage("카메라 권한 설정이 필요합니다")
.setDeniedMessage("거부 하셨습니다")
.setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
.check();
}
kotlin
private fun setPermission(){
**var permissionListener : PermissionListener = object : PermissionListener** {
override fun onPermissionGranted() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, CAMERA_CODE) // -- 3번
}
override fun onPermissionDenied(deniedPermissions: MutableList<String>?) {
Toast.makeText(mContext, "거부하셨습니다", Toast.LENGTH_SHORT).show()
}
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
TedPermission.with(mContext)
.setPermissionListener(permissionListener)
.setRationaleMessage("카메라 권한 설정이 필요합니다")
.setDeniedMessage("거부 하셨습니다")
.setPermissions(Manifest.permission.CAMERA)
.check()
} else {
// 정의해줘야함 -> 권한 승인됐을때 함수때리면 됨
}
}