Get string value by key from SharedPreferences - Android Android OS

Android examples for Android OS:SharedPreferences

Description

Get string value by key from SharedPreferences

Demo Code


//package com.java2s;

import android.content.Context;
import android.content.SharedPreferences;

public class Main {
    public static final String KEY = "org.java2s";
    private static Context mContext;

    public static String getShareData(String key) {
        SharedPreferences sp = mContext.getSharedPreferences(KEY,
                Context.MODE_PRIVATE);
        return sp.getString(key, "");
    }/*from   w w  w.j av a2s.  c o m*/

    public static String getShareData(String key, String defValue) {
        SharedPreferences sp = mContext.getSharedPreferences(KEY,
                Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }
}

Related Tutorials