반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- v-text-field height
- NUXT
- CSS
- 프로세스 방지
- xlsx
- 동일 프로세스
- onsen-ui
- vuejs
- MySQL
- electron-nuxt
- sort
- Vuetify
- v-select
- bucket max-key
- c#
- bucket cors
- JavaScript
- Electron
- naver storage bucket error
- sequelize
- vuetifyjs
- nodejs
- naver storage
- onsenui
- 동일 프로그램
- f035d
- Vue
- error
- kotlin
- Android
Archives
- Today
- Total
앙큼한 개발기록
[android] kotlin에서 SharedPreference 사용하기 본문
안드로이드에서 자동 로그인, 간단한 정보를 저장할때
사용되는 sharedpreperence를 이번에
object로 정리해 보았다.
SharedPreferenceAccount.kt
object SharedPreferenceAccount {
const val ACCOUNT: String = "account"
const val TOKEN: String = "account_token"
const val IS_LOGIN: String = "account_is_login"
fun setString(context: Context, key: String, value: String) {
val prefs : SharedPreferences = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE)
val editor : SharedPreferences.Editor = prefs.edit()
editor.putString(key, value)
editor.apply()
}
fun setBoolean(context: Context, key: String, value: Boolean) {
val prefs : SharedPreferences = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE)
val editor : SharedPreferences.Editor = prefs.edit()
editor.putBoolean(key, value)
editor.apply()
}
fun setDateToString(context: Context, key: String, date: Date) {
val prefs: SharedPreferences = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE)
val editor: SharedPreferences.Editor = prefs.edit()
val dateString = Common.dateTimeFormat(date)
editor.putString(key, dateString)
editor.apply()
}
fun getString(context: Context, key: String): String {
val prefs : SharedPreferences = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE)
return prefs.getString(key, "").toString()
}
fun getBoolean(context: Context, key: String): Boolean {
val prefs: SharedPreferences = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE)
return prefs.getBoolean(key, false)
}
fun clear(context: Context) {
val prefs : SharedPreferences = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE)
val editor : SharedPreferences.Editor = prefs.edit()
editor.clear()
editor.apply()
val token = this.getString(context, TOKEN)
if (token.isNotBlank()) {
this.setString(context, TOKEN, token)
}
this.setBoolean(context, IS_LOGIN, false)
}
}
token 값과 is_login은 별도로 구분해서 저장하였는데
인터넷이 끊겼을 때 로그아웃 하고나서 다시 로그인인을 시도 하거나
이전에 로그인한지 안한지를 체크 하기 위한 값으로 활용 하였다.
끝.
'개발 > android' 카테고리의 다른 글
[android] kotlin 파일 저장 및 호출 방법 (0) | 2023.05.17 |
---|---|
[android] app (update & delete)시 데이터 유지 (버전관리) (0) | 2023.05.16 |
[android] material datepicker 달력 한글 (0) | 2023.04.02 |
firebase android connect 파이어베이스 안드로이드 연결 (3) | 2018.01.21 |
firebase 데이터 구조 (0) | 2018.01.21 |
Comments