Android SharedPreferences Get getPrefInt(SharedPreferences pref, String key, int default_value)

Here you can find the source of getPrefInt(SharedPreferences pref, String key, int default_value)

Description

get Pref Int

Declaration

public static int getPrefInt(SharedPreferences pref, String key,
            int default_value) 

Method Source Code

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.preference.PreferenceManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main{
    public static int getPrefInt(SharedPreferences pref, String key,
            int default_value) {
        Object value = getPrefObject(pref, key);
        int ret = default_value;
        if (value != null && value instanceof String) {
            String str = (String) value;
            if (MyStringUtlis.isEmpty(str) == false) {
                try {
                    ret = Integer.parseInt(str);
                } catch (Throwable e) {
                    Logger.w("invalid value, key=" + key + ", value=" + str);
                }/*from   w w  w. j  ava 2s  . c o  m*/
            }
        }
        return ret;
    }
    public static int getPrefInt(SharedPreferences pref, String key,
            String default_value) {
        int ret = getPrefInt(pref, key, Integer.MIN_VALUE);
        if (ret == Integer.MIN_VALUE) {
            ret = Integer.parseInt(default_value);
        }
        return ret;
    }
    protected static Object getPrefObject(SharedPreferences pref, String key) {

        Map<String, ?> all = pref.getAll();
        if (all != null) {
            Object obj = all.get(key);
            return obj;
        }

        return null;
    }
}

Related

  1. getPrefBoolean(SharedPreferences pref, String key)
  2. getPrefBoolean(SharedPreferences pref, String key)
  3. getPrefInt(SharedPreferences pref, String key, String default_value)
  4. getPrefObject(SharedPreferences pref, String key)
  5. getPrefObject(SharedPreferences pref, String key)
  6. getPrefBoolean(Context c, String key, boolean defValue)
  7. getPrefInt(Context c, String key, int defValue)