App노자
[Android] Preference란 본문
1.Preference란?
플랫폼API에서 제공하는 클래스로 데이터를 키 - 값 형태로 보관한다
간단한 데이터를 저장하는데 사용하며 내장 메모리 앱 폴더에 XML파일로 저장되며
getSharedPreferences(), getPreferences() 2가지를 제공한다
getSharedPreferences() 앱 전체의 데이터를 키 -값 형태로 저장할때 사용한다
첫 번째 매개변수에 지정한 이름의 파일로 저장이 되며 앱의 모든 Context에서 이 메서드를 호출할 수 있다
getPreferences()
https://developer.android.com/training/data-storage/shared-preferences?hl=ko
키-값 데이터 저장 | Android 개발자 | Android Developers
키-값 데이터 저장 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 저장하려는 키-값 컬렉션이 비교적 작은 경우 SharedPreferences API를 사용해야 합니다. SharedPre
developer.android.com
2.Preference란?
package jp.co.i_bec.happymailapp.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import jp.co.i_bec.happymailapp.constants.AppConstants;
/**
* Created by decoo on 2014/06/23.
*/
public class PreferenceUtils {
private static SharedPreferences myPref;
/*
* getSharedPreferencesの設定
*/
private static void setMyPref(final Context context) {
myPref = context.getSharedPreferences(AppConstants.APP_PREF_NAME, Context.MODE_PRIVATE);
}
/*
* 指定設定情報の更新(boolean)
*/
public static void updatePreference(final Context context, final String myKey, boolean myValue) {
try {
//設定されていない場合のみPrefを設定
if (myPref == null) {
setMyPref(context);
}
//指定Prefを更新
Editor Ed = myPref.edit();
Ed.putBoolean(myKey, myValue);
Ed.commit();
} catch (Exception e) {
LogUtils.e(AppConstants.TAG, "********************** updatePreference:_@" + Log.getStackTraceString(e));
}
}
public static void deletePreference(final Context context, final String myKey) {
try {
if (myPref == null) setMyPref(context);
Editor Ed = myPref.edit();
Ed.remove(myKey);
Ed.commit();
} catch (Exception e) {
LogUtils.e(AppConstants.TAG, "**********************:deletePreference_@" + Log.getStackTraceString(e));
} finally {
}
}
/*
* 指定設定情報の更新(int)
*/
public static void updatePreference(final Context context, final String myKey, int myValue) {
//設定されていない場合のみPrefを設定
if (myPref == null) {
setMyPref(context);
}
//指定Prefを更新
Editor Ed = myPref.edit();
Ed.putInt(myKey, myValue);
Ed.commit();
}
/*
* 指定設定情報の更新(long)
*/
public static void updatePreference(final Context context, final String myKey, long myValue) {
//設定されていない場合のみPrefを設定
if (myPref == null) {
setMyPref(context);
}
//指定Prefを更新
Editor Ed = myPref.edit();
Ed.putLong(myKey, myValue);
Ed.commit();
}
/*
* 指定設定情報の更新(String)
*/
public static void updatePreference(final Context context, final String myKey, final String myValue) {
//設定されていない場合のみPrefを設定
if (myPref == null) {
setMyPref(context);
}
//指定Prefを更新
Editor Ed = myPref.edit();
Ed.putString(myKey, myValue);
Ed.commit();
}
/*
* 指定設定情報取得(boolean)
*/
public static boolean getPreference(final Context context, final String myKey, boolean defValue) {
boolean myResult = false;
//設定されていない場合のみPrefを設定
if (myPref == null) {
setMyPref(context);
}
myResult = myPref.getBoolean(myKey, defValue);
return myResult;
}
/*
* 指定設定情報取得(文字列)
*/
public static String getPreference(final Context context, final String myKey, final String defValue) {
String myResult = "";
try {
//設定されていない場合のみPrefを設定
if (myPref == null) {
setMyPref(context);
}
myResult = myPref.getString(myKey, defValue);
} catch (Exception e) {
LogUtils.e("Mylog", "" + ":" + Log.getStackTraceString(e));
}
return myResult;
}
/*
* 指定設定情報取得(数値)
*/
public static int getPreference(final Context context, final String myKey, int defValue) {
int myResult = 0;
try {
//設定されていない場合のみPrefを設定
if (myPref == null) {
setMyPref(context);
}
myResult = myPref.getInt(myKey, defValue);
} catch (Exception e) {
LogUtils.e("Mylog", "getPreference_Int" + ":" + Log.getStackTraceString(e));
}
return myResult;
}
/*
* 指定設定情報取得(数値)
*/
public static long getPreference(final Context context, final String myKey, long defValue) {
long myResult = 0;
try {
//設定されていない場合のみPrefを設定
if (myPref == null) {
setMyPref(context);
}
myResult = myPref.getLong(myKey, defValue);
} catch (Exception e) {
LogUtils.e("Mylog", "getPreference_Long" + ":" + Log.getStackTraceString(e));
}
return myResult;
}
public static String sendAndroidCrash(Context context, Throwable sendException) {
String str = "";
try {
final SharedPreferences pref = context.getSharedPreferences(AppConstants.APP_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
str = CrashReportUtils.convertThrowableToErrorLog(sendException);
editor.putString(CrashReportUtils.PREF_KEY, str);
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
'Android > AndroidStudio' 카테고리의 다른 글
[Android] RecyclerView란 (0) | 2023.03.25 |
---|---|
[Android] ListView란 (0) | 2023.03.18 |
[Android] Intent란 (0) | 2023.03.05 |
[Android] RingtoneManager란 (0) | 2023.03.01 |
[Android] AudioManager란 (2) | 2023.02.15 |