개발노트/Android
[Kotlin] 앱 알림 설정 열기
전지적진영시점
2023. 11. 22. 22:40
개발 환경
---------------------------------
OS : Mac
개발 툴 : Android Studio
개발 언어 : Kotlin
targetSdk : 34
minSdk : 23
---------------------------------
이번 포스팅에서는 android 앱에서
앱 설정 화면을 열어보겠다.
Oreo 버전을 기점으로 이전 버전과 이후 버전의 처리 방식이 다르다.
구분해서 작업해주면 된다.
fun loadSetting() {
val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationSettingOreo(this@MainActivity)
} else {
notificationSettingOreoLess(this@MainActivity)
}
try {
this@MainActivity.startActivity(intent)
}catch (e: ActivityNotFoundException) {
e.printStackTrace()
}
}
@RequiresApi(Build.VERSION_CODES.O)
fun notificationSettingOreo(context: Context): Intent {
return Intent().also { intent ->
intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
}
fun notificationSettingOreoLess(context: Context): Intent {
return Intent().also { intent ->
intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
intent.putExtra("app_package", context.packageName)
intent.putExtra("app_uid", context.applicationInfo?.uid)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
}