1. Concepts of Permission
2. Permission을 적용하는 애플리케이션 코드(위의 예시에서 A App)
<permission android:name="com.example.permission.TEST_PERMISSION"
android:label="Test Permission"
android:description="@string/permission_des"
android:protectionLevel="dangerous"/>
<activity
android:name=".MainActivity"
android:exported="true"
android:permission="com.example.permission.TEST_PERMISSION"
</activity>
<activity
android:name=".MainActivity"
android:exported="true"
</activity>
3. Permission을 이용하는 애플리케이션 코드(위의 예시에서 B App)
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
4. 시스템에서 요구되는 퍼미션
1. Permission Check
val status = ContextCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION")
if(status == PackageManager.PERMISSION_GRANTED){
Log.d("permission check", "permission granted")
// 기능 실행
} else{
Log.d("permission check", "permission granted")
// 기능 실행 시키면 에러, 유저에게 퍼미션 부여 요청 → Permission Request
}
2. Permission Request
(1) requestPermissions() 사용
ActivityCompat.requestPermissions(this, arrayOf<String>("android.permission.ACCESS_FINE_LOCATION"), 100)
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray){
super.onRequestPermissionsResult(requestCode, permissions, grantResult)
if(grantResult[0]==PackageManager.PERMISSION_GRANTED_{
Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show()
} else{
Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show()
}
}
(2) registerForActivityResult() 사용
requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) {isGranted ->
if(isGranted) {
Toast.makeText(this, "permission granted2", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "permission granted2", Toast.LENGTH_SHORT).show()
}
}