쉽고 간단하게 사용할 수 있는 Runtime Permission Library
Dependency
dependencies{ implementation 'com.karumi:dexter:6.2.1' }
AndroidManifest
AndroidManifest에 필요한 uses-permission 추가
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your package name" > <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.CAMERA"/> ... </manifest>
class MainActivity : AppCompatActivity(), PermissionListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) Dexter.withContext(this) .withPermission(Manifest.permission.CAMERA) .withListener(this) .check() } override fun onPermissionGranted(response: PermissionGrantedResponse) { // This method will be called when the permission is denied } override fun onPermissionRationaleShouldBeShown(permission: PermissionRequest, token: PermissionToken?) { // This method will be called when the user rejects a permission request // You must display a dialog box that explains to the user why the application needs this permission } override fun onPermissionDenied(response: PermissionDeniedResponse) { // This method will be called when the permission is granted } }
class MainActivity : AppCompatActivity(), MultiplePermissionsListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val permissions = listOf( Manifest.permission.CAMERA, Manifest.permission.SEND_SMS ) Dexter.withContext(this) .withPermissions(permissions) .withListener(this) .check() } override fun onPermissionRationaleShouldBeShown(permissions: MutableList<PermissionRequest>, token: PermissionToken) { // This method will be called when the user rejects a permission request // You must display a dialog box that explains to the user why the application needs this permission } override fun onPermissionsChecked(report: MultiplePermissionsReport) { // Here you have to check granted permissions } }