-
-
Save U-WangE/1295f871793bf7612e3497292d200515 to your computer and use it in GitHub Desktop.
[AccessPermission] 위치 권한을 받기 위한 방법이고, 아래 방법을 사용해서 다른 권한을 받을 수도 있다. accessPermission + onRequestPermissionResoult + onRestart 는 같은 Class , showSnackBar은 Module이다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressLint("NewApi") | |
private fun accessPermission() { | |
with(Build.VERSION.SDK_INT) { | |
when { | |
this >= Build.VERSION_CODES.M -> { | |
if (checkSelfPermission(ACCESS_FINE_LOCATION) != PERMISSION_GRANTED || | |
checkSelfPermission(ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED | |
) { | |
ActivityCompat.requestPermissions( | |
this@SplashActivity, | |
arrayOf( | |
ACCESS_FINE_LOCATION, | |
ACCESS_COARSE_LOCATION | |
), | |
REQUEST_CODE_ACCESS // 상수 아무거나 | |
) | |
} else { | |
// permission accept | |
} | |
} | |
// this >= Build.VERSION_CODES.* 추가 버전 별 permission 설정 가능 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onRequestPermissionsResult( | |
requestCode: Int, | |
permissions: Array<out String>, | |
grantResults: IntArray | |
) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
grantResults | |
.contains(PERMISSION_GRANTED) | |
.let { | |
if (it) { | |
// 해당 권한 Accept -> 다른 권한 설정 or Intent | |
when (requestCode) { | |
REQUEST_CODE_ACCESS -> { | |
permissionCheckNow = REQUEST_CODE_STORAGE | |
storagePermission() | |
} | |
REQUEST_CODE_STORAGE -> { | |
permissionCheckNow = REQUEST_CODE_BLUETOOTH | |
bluetoothAndAccessPermission() | |
} | |
REQUEST_CODE_BLUETOOTH -> { | |
permissionCheckNow = REQUEST_CODE_ACCESS | |
loginCheck() | |
} | |
} | |
} else permissions.all { | |
// 1. 사용자가 권한 요청을 명시적으로 거부한 경우 true를 반환한다. | |
// 2. 사용자가 권한 요청을 처음 보거나, 다시 묻지 않음 선택한 경우, 권한을 허용한 경우 false를 반환한다. | |
// 이곳에는 2.의 두 번 거절, 다시 묻지 않음과 1.번만 들어온다. (처음 권한 요청, 권한 허용의 경우 위에서 걸러짐) | |
ActivityCompat.shouldShowRequestPermissionRationale(this@SplashActivity, it) | |
}.let { | |
// 권한 요청 재시도 | |
if (it) { | |
when (requestCode) { | |
REQUEST_CODE_ACCESS -> accessPermission() | |
REQUEST_CODE_STORAGE -> storagePermission() | |
REQUEST_CODE_BLUETOOTH -> bluetoothAndAccessPermission() | |
} | |
} | |
// 다시 묻지 않음을 선택하거나, 두 번 거절한 경우 | |
// snackBar 띄움 | |
else { | |
// snackBar Module | |
showSnackBar(binding.root, getString(R.string.need_permission)) { | |
startActivity(Intent().apply { | |
when (requestCode) { | |
REQUEST_CODE_ACCESS -> action = | |
ACTION_APPLICATION_DETAILS_SETTINGS | |
REQUEST_CODE_STORAGE -> action = | |
ACTION_APPLICATION_DETAILS_SETTINGS | |
REQUEST_CODE_BLUETOOTH -> action = | |
ACTION_APPLICATION_DETAILS_SETTINGS | |
} | |
data = Uri.fromParts("package", packageName, null) | |
}) | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 앱 외부 환경설정 창에서 permission 설정 안 하고, 취소를 누른 경우 | |
override fun onRestart() { | |
super.onRestart() | |
when(permissionCheckNow) { | |
REQUEST_CODE_ACCESS -> accessPermission() | |
REQUEST_CODE_STORAGE -> storagePermission() | |
REQUEST_CODE_BLUETOOTH -> bluetoothAndAccessPermission() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun showSnackBar(view: View, message: String, navigation: () -> Unit) { | |
Snackbar.make( | |
view, | |
message, | |
Snackbar.LENGTH_INDEFINITE | |
).apply { | |
setAction(context.getString(R.string.ok_button)) { | |
navigation.invoke() | |
} | |
show() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Time 1D - 2D + Custom 1D - 2D