Android Application
작성자 임베디드코리아
작성일 23-10-06 22:17
조회1,642회
댓글0건
-------------------------------------------------------------------------------------
MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var permissionLauncher : ActivityResultLauncher<Array<String>>
private var isAccessCoarseLocationPermissionGranted = false
private var isReadPermissionGranted = false
private var isWritePermissionGranted = false
private var isLocationPermissionGranted = false
private var isRecordAudioPermissionGranted = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
permissionLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){ permissions ->
isAccessCoarseLocationPermissionGranted = permissions[android.Manifest.permission.ACCESS_COARSE_LOCATION] ?: isAccessCoarseLocationPermissionGranted
isReadPermissionGranted = permissions[android.Manifest.permission.ACCESS_FINE_LOCATION] ?: isReadPermissionGranted
isWritePermissionGranted = permissions[android.Manifest.permission.READ_EXTERNAL_STORAGE] ?: isWritePermissionGranted
isLocationPermissionGranted = permissions[android.Manifest.permission.WRITE_EXTERNAL_STORAGE] ?: isLocationPermissionGranted
isRecordAudioPermissionGranted = permissions[android.Manifest.permission.RECORD_AUDIO] ?: isRecordAudioPermissionGranted
//자리에 맞게 재요청 할 내용 Toast표시 및 requestPermission다시 보내기 등 하면 되는 자리
}
selfCheckPermission()
requestPermission()
}
//권한 없는것 요청
private fun requestPermission() {
val permissionRequest : MutableList<String> = ArrayList()
if(!isAccessCoarseLocationPermissionGranted){ permissionRequest.add(android.Manifest.permission.ACCESS_COARSE_LOCATION) }
if(!isLocationPermissionGranted) { permissionRequest.add(android.Manifest.permission.ACCESS_FINE_LOCATION) }
if(!isReadPermissionGranted) { permissionRequest.add(android.Manifest.permission.READ_EXTERNAL_STORAGE) }
if(!isWritePermissionGranted) { permissionRequest.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) }
if(!isRecordAudioPermissionGranted) { permissionRequest.add(android.Manifest.permission.RECORD_AUDIO) }
//없는 권한 ArrayList로 만들어 Launch로 MultiPermission열기
if(permissionRequest.isNotEmpty()){permissionLauncher.launch(permissionRequest.toTypedArray())}
}
//권한 등록되어있는지 체크
private fun selfCheckPermission() {
//위치 기반 권한 체크
isAccessCoarseLocationPermissionGranted = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED
isLocationPermissionGranted = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
//저장소 일기 권한 체크
isReadPermissionGranted = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
//저장소 쓰기 권한 체크
val isWritePermission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
val minSdkLevel = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q //쓰기 권한은 28이후에 나왔으므로
isWritePermissionGranted = isWritePermission || minSdkLevel
//오디오
isRecordAudioPermissionGranted = ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO
) == PackageManager.PERMISSION_GRANTED
}
}
------------------------------------------------------------------------------------
*** AndroidManifest.xml
------------------------------------------------------------------------------------
<!--추가한 부분-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/> <!-- 28 이상은 쓰기 권한 을 받을 필요가 없다-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="true"/>
<!---->
158-840 서울시 양천구 남부순환로 571(신월동, 영남타운 307호) l 대표: 박길성 ㅣ Tel:02-2695-1114 ㅣ Fax:02-2695-1113
Copyright © 2015 Embedded Korea. All Rights Reserved.