안드로이드 Q에서 위치 권한에 대한 변화가 있었습니다. 개인정보를 강화하기 위해, 기존의 위치 퍼미션(ACCESS_FINE_LOCATION 또는 ACCESS_COARSE_LOCATION)은 앱이 Foreground에 실행 중일 때만 위치 정보를 얻을 수 있습니다. 만약 Background에서 디바이스의 위치 정보를 얻고 싶다면 신규 권한인 ACCESS_BACKGROUND_LOCATION을 별도로 요청해야 합니다.
이제 앱이 위치 권한을 요청하면 사용자는 아래와 같은 세분화된 위치 권한을 부여할 수 있는 UI를 보게 됩니다.
Allow all the time은 기존의 위치 권한과 백그라운드 위치 권한을 모두 부여하는 것이고, Allow only while using the app은 백그라운드에서 위치 권한을 사용하지 못하도록 Foreground 위치 권한만 부여하는 것입니다.
앱이 Foreground로 동작하고 있다는 것은 앱의 Activity가 실행 중이거나, Foreground Service가 동작하고 있는 상태입니다. 그 외의 동작은 Background로 간주합니다.
항상 위치 정보가 필요할 때
Q를 타겟으로 하는 앱이 항상(Background, Foreground) 위치 정보를 얻고 싶으면 AndroidManifest.xml에 다음과 같이 권한을 요청해야 합니다. 두개의 권한을 모두 받으면 앱은 항상 위치 정보를 얻을 수 있습니다.
<manifest>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
</manifest>자세한 구현 내용은 구글 샘플을 참고해주세요
앱이 실행 중일 때만 위치 정보가 필요할 때
앱이 실행 중일 때만 위치 정보가 필요하다면 Background Location 권한을 요청할 필요가 없습니다.
<manifest>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>만약 Foreground 서비스에서 위치 정보를 얻어야 한다면, 추가로 권한을 요청할 필요는 없지만 별도의 세팅을 해줘야 합니다. 아래 코드와 같이 위치 정보에 접근하는 Foreground 서비스의 manifest에 foregroundServiceType="location"을 선언해줘야 합니다.
<service
android:foregroundServiceType="location" .../>자세한 구현 내용은 구글 샘플을 참고해주세요
위치 권한 요청
위치 권한을 요청할 때는 액티비티가 실행되었을 때 하는 것이 좋습니다. 백그라운드에서도 권한 요청을 할 수 있지만 앱을 사용하지 않는데 갑자기 권한 요청 팝업이 뜬다면 사용자는 권한을 주지 않거나 포어그라운드에서만 사용하도록 권한을 축소시킬 수 있습니다.
따라서, Background 서비스에서 요청팝업을 띄우지 않으려면 실행하기 전에 권한을 미리 체크해야 합니다. 다음 코드는 권한 요청 및 케이스별로 분기되는 예제입니다.
val permissionAccessCoarseLocationApproved = ActivityCompat
.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED
if (permissionAccessCoarseLocationApproved) {
val backgroundLocationPermissionApproved = ActivityCompat
.checkSelfPermission(this, permission.ACCESS_BACKGROUND_LOCATION) ==
PackageManager.PERMISSION_GRANTED
if (backgroundLocationPermissionApproved) {
// App can access location both in the foreground and in the background.
// Start your service that doesn't have a foreground service type
// defined.
} else {
// App can only access location in the foreground. Display a dialog
// warning the user that your app must have all-the-time access to
// location in order to function properly. Then, request background
// location.
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION),
your-permission-request-code
)
}
} else {
// App doesn't have access to the device's location at all. Make full request
// for permission.
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION),
your-permission-request-code
)
}Q 타겟으로 업그레이드
P 이하 버전을 타겟으로 한 앱이 Q 디바이스에서 동작할 수 있는데요. 이 앱이 위치 퍼미션을 사용하고 있다면 시스템은 자동으로 ACCESS_BACKGROUND_LOCATION도 요청하도록 변경해 줍니다. 그렇기 때문에 급하게 업데이트를 하지 않아도 됩니다.
Q를 타겟으로 한 앱처럼 Background Location 권한을 부여 받을 수 있습니다.
Q로 업그레이드 될 때, 위의 가이드 처럼 앱의 매니페스트와 권한 요청 코드를 변경해주시면 됩니다.
정리
Q의 변경된 위치 권한 정책에 대해서 알아보았습니다. 이번 정책으로 앱은 무분별하게 위치 정보를 가져 갈 수 없게 되었고, 사용자는 자신의 위치 정보가 언제 앱에 제공되는지 명시적으로 인지할 수 있게 되었습니다.
참고
Related Posts
- Android 14 - 사진/동영상 파일, 일부 접근 권한 소개
- Android - adb push, pull로 파일 복사, 다운로드
- Android 14 - 암시적 인텐트 변경사항 및 문제 해결
- Jetpack Compose - Row와 Column
- Android 13, AOSP 오픈소스 다운로드 및 빌드
- Android 13 - 세분화된 미디어 파일 권한
- Android 13에서 Notification 권한 요청, 알림 띄우기
- Android 13에서 'Access blocked: ComponentInfo' 에러 해결
- 에러 해결: android gradle plugin requires java 11 to run. you are currently using java 1.8.
- 안드로이드 - 코루틴과 Retrofit으로 비동기 통신 예제
- 안드로이드 - 코루틴으로 URL 이미지 불러오기
- Android - 진동, Vibrator, VibrationEffect 예제
- Some problems were found with the configuration of task 에러 수정
- Query method parameters should either be a type that can be converted into a database column or a List
- 우분투에서 Android 12 오픈소스 다운로드 및 빌드
- Android - ViewModel을 생성하는 방법
- Android - Transformations.map(), switchMap() 차이점
- Android - Transformations.distinctUntilChanged() 소개
- Android - TabLayout 구현 방법 (+ ViewPager2)
- Android - 휴대폰 전화번호 가져오는 방법
- Android 12 - Splash Screens 알아보기
- Android 12 - Incremental Install (Play as you Download) 소개
- Android - adb 명령어로 bugreport 로그 파일 추출
- Android - adb 명령어로 App 데이터 삭제
- Android - adb 명령어로 앱 비활성화, 활성화
- Android - adb 명령어로 특정 패키지의 PID 찾기
- Android - adb 명령어로 퍼미션 Grant 또는 Revoke
- Android - adb 명령어로 apk 설치, 삭제
- Android - adb 명령어로 특정 패키지의 프로세스 종료
- Android - adb 명령어로 screen capture 저장
- Android - adb 명령어로 System 앱 삭제, 설치
- Android - adb 명령어로 settings value 확인, 변경
- Android 12 - IntentFilter의 exported 명시적 선언
- Android - adb 명령어로 공장초기화(Factory reset)
- Android - adb logcat 명령어로 로그 출력