티스토리 뷰

Rest Api를 통해 서버와 통신을 하여 앱을 만들고 있는데 이때 로그인 시 토큰을 발행하여 사용자 인증을 하도록 회의를 진행하였다.

 

이때 토큰은 로그인 액티비티에서 발행을 하지만 실제 사용되는 액티비티는 다르기 때문에 어떻게 하면 불러올까 찾아보다 처음 접한 방법은 Intent를 통해 받아오는 방법이 나왔다. 하지만 제대로 작동하지 않았고 제한이 많아서 

구글링을 하다보니 나온 방식이

 

PreferenceManager를 통해서 앱 내부에 파일을 생성하여 그 파일내에 값들을 저장하는 방식이었다.

 

일단 PreferenceManager를 사용하기 위해서는 Java 클래스를 하나 생성해야 한다.

 

package com.cookandroid.users;
import android.content.Context;
import android.content.SharedPreferences;

public class PreferenceManager {
    public static final String PREFERENCES_NAME = "rebuild_preference";



    private static final String DEFAULT_VALUE_STRING = "";

    private static final boolean DEFAULT_VALUE_BOOLEAN = false;

    private static final int DEFAULT_VALUE_INT = -1;

    private static final long DEFAULT_VALUE_LONG = -1L;

    private static final float DEFAULT_VALUE_FLOAT = -1F;



    private static SharedPreferences getPreferences(Context context) {

        return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);

    }



    /**

     * String 값 저장

     * @param mContext

     * @param key

     * @param value

     */

    public static void setString(Context mContext, String key, String value) {

        SharedPreferences prefs = getPreferences(mContext);

        SharedPreferences.Editor editor = prefs.edit();

        editor.putString(key, value);

        editor.apply();

    }



    /**

     * boolean 값 저장

     * @param context

     * @param key

     * @param value

     */

    public static void setBoolean(Context context, String key, boolean value) {

        SharedPreferences prefs = getPreferences(context);

        SharedPreferences.Editor editor = prefs.edit();

        editor.putBoolean(key, value);

        editor.apply();

    }



    /**

     * int 값 저장

     * @param context

     * @param key

     * @param value

     */

    public static void setInt(Context context, String key, int value) {

        SharedPreferences prefs = getPreferences(context);

        SharedPreferences.Editor editor = prefs.edit();

        editor.putInt(key, value);

        editor.apply();

    }



    /**

     * long 값 저장

     * @param context

     * @param key

     * @param value

     */

    public static void setLong(Context context, String key, long value) {

        SharedPreferences prefs = getPreferences(context);

        SharedPreferences.Editor editor = prefs.edit();

        editor.putLong(key, value);

        editor.apply();

    }



    /**

     * float 값 저장

     * @param context

     * @param key

     * @param value

     */

    public static void setFloat(Context context, String key, float value) {

        SharedPreferences prefs = getPreferences(context);

        SharedPreferences.Editor editor = prefs.edit();

        editor.putFloat(key, value);

        editor.apply();

    }



    /**

     * String 값 로드

     * @param context

     * @param key

     * @return

     */

    public static String getString(Context context, String key) {

        SharedPreferences prefs = getPreferences(context);

        String value = prefs.getString(key, DEFAULT_VALUE_STRING);

        return value;

    }



    /**

     * boolean 값 로드

     * @param context

     * @param key

     * @return

     */

    public static boolean getBoolean(Context context, String key) {

        SharedPreferences prefs = getPreferences(context);

        boolean value = prefs.getBoolean(key, DEFAULT_VALUE_BOOLEAN);

        return value;

    }



    /**

     * int 값 로드

     * @param context

     * @param key

     * @return

     */

    public static int getInt(Context context, String key) {

        SharedPreferences prefs = getPreferences(context);

        int value = prefs.getInt(key, DEFAULT_VALUE_INT);

        return value;

    }



    /**

     * long 값 로드

     * @param context

     * @param key

     * @return

     */

    public static long getLong(Context context, String key) {

        SharedPreferences prefs = getPreferences(context);

        long value = prefs.getLong(key, DEFAULT_VALUE_LONG);

        return value;

    }



    /**

     * float 값 로드

     * @param context

     * @param key

     * @return

     */

    public static float getFloat(Context context, String key) {

        SharedPreferences prefs = getPreferences(context);

        float value = prefs.getFloat(key, DEFAULT_VALUE_FLOAT);

        return value;

    }



    /**

     * 키 값 삭제

     * @param context

     * @param key

     */

    public static void removeKey(Context context, String key) {

        SharedPreferences prefs = getPreferences(context);

        SharedPreferences.Editor edit = prefs.edit();

        edit.remove(key);

        edit.apply();

    }



    /**

     * 모든 저장 데이터 삭제

     * @param context

     */

    public static void clear(Context context) {

        SharedPreferences prefs = getPreferences(context);

        SharedPreferences.Editor edit = prefs.edit();

        edit.clear();

        edit.apply();

    }

}

이 클래스에는 PreferenceManager에 값을 저장하는 방법과 불러오는 방법이 정의되어있다.

 

내가 이해한 방법은 context는 리모콘 "key"는 버튼이다.

즉 리모콘과 버튼을 누르면 그에 해당하는 값이 불러와지고 저장되는 방식이다.

 

PreferenceManager를 사용하기 위해서는 사용할 액티비티에 Context를 생성하고 onCreate() 안에 

cotext = this; 를 선언해주어야 한다.

 

PreferenceManager.setString(mContext, "key", authToken);

토큰 저장

 

String token = PreferenceManager.getString(mContext, "key");

토큰 불러오기

 

PreferenceManager는 앱을 삭제하면 파일이 날아가서 그 안에 내용이 전부 없어진다.

토큰같이 중요한 파일이나 서버에 올리기는 애매한 값들은 PreferenceManager를 통해서 저장하고 불러올 수 있다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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 31
글 보관함