package yhkj.transition.supplytransition.utils; import android.R.anim; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.DialogInterface; 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.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.location.LocationManager; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.Settings; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Base64; import android.util.Log; import android.view.WindowManager; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.TimeZone; /** * Activity工具类 * * @author liupeng */ public class ActivityUtils { // private static Dialog builder; private static ProgressDialog m_pDialog; private static ActivityUtils aUtility; private static Context ctx; /** * 是否处于调试或者开发模式 */ public static boolean ISDEBUG = false; /** * 初始化方法,只有一个实体类存在 * * @param context * @return */ public static ActivityUtils init(Context context) { if (aUtility == null) { aUtility = new ActivityUtils(context); } else { if (null != context) { aUtility.ctx = context; } } return aUtility; } /** * 构造方法 */ private ActivityUtils(Context context) { // TODO Auto-generated constructor stub this.ctx = context; ISDEBUG = isApkDebugable(context); } private SharedPreferences sp; /** * 根据指定的name获取SharedPreferences * * @param filename 指定的文件名称 * @return */ public ActivityUtils SP(String filename) { sp = ctx.getSharedPreferences(filename, Context.MODE_PRIVATE); return this; } /** * put data to SharedPreferences * * @param key 键 * @param value 值 */ @SuppressWarnings("unchecked") public void putSP(String key, Object value) { if (null == sp) { return; } Editor editor = sp.edit(); if (value instanceof String) { editor.putString(key, (String) value); Log.i("liupeng","key --> "+key + " value --> "+value); } else if (value instanceof Boolean) { editor.putBoolean(key, (boolean) value); } else if (value instanceof Integer) { editor.putInt(key, (int) value); } else if (value instanceof Float) { editor.putFloat(key, (float) value); } else if (value instanceof Long) { editor.putLong(key, (long) value); } else if (value instanceof Set<?>) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { editor.putStringSet(key, (Set<String>) value); } else { System.out.println(Build.VERSION.SDK_INT + "is to low,put data failed"); } } editor.apply(); // editor.commit(); } /** * get data from SharedPreferences * * @param key 键 * @param clazz 指定需要获取的数据的类型 * @return */ @SuppressWarnings("unchecked") public <T> T getSP(String key, Class<T> clazz) { if (null == sp) { return null; } if ((String.class).equals(clazz)) { return (T) sp.getString(key, null); } else if ((Integer.class).equals(clazz)) { return (T) Integer.valueOf(sp.getInt(key, Integer.MIN_VALUE)); } else if ((Float.class).equals(clazz)) { return (T) Float.valueOf(sp.getFloat(key, Integer.MIN_VALUE)); } else if ((Long.class).equals(clazz)) { return (T) Long.valueOf(sp.getLong(key, Integer.MIN_VALUE)); } else if ((Boolean.class).equals(clazz)) { return (T) Boolean.valueOf(sp.getBoolean(key, false)); } else if ((Set.class).equals(clazz)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { return (T) sp.getStringSet(key, null); } else { System.out.println(Build.VERSION.SDK_INT + "is to low,get data failed"); } } return null; } private static final String DEVICE = "device"; private static final String DEVICEID = "deviceId"; /** * 统一将当前设备编号写入至SharedPreferences */ public String putDeviceID() { TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); String deviceId = tm.getDeviceId(); SP(DEVICE).putSP(DEVICEID, deviceId); return deviceId; } /** * 统一从SharedPreferences获取当前设备编号 * * @return */ public String getDeviceID() { String deviceId = SP(DEVICE).getSP(DEVICEID, String.class); if (TextUtils.isEmpty(deviceId)) {//检查一遍是否启动没有put进去 deviceId = putDeviceID(); } return deviceId; } private static final String USER = "USER"; private static final String USERID = "USERID"; private static final String ROLEID = "ROLEID"; private static final String TrueName = "TrueName"; private static final String STORAGE = "STORAGE"; private static final String CUSTOMER = "CUSTOMER"; private static final String TEL = "TEL"; private static final String ADDRESS = "ADDRESS"; public int getUserID() { Integer userID = SP(USER).getSP(USERID, Integer.class); return userID; } public void putUserID(int userId) { SP(USER).putSP(USERID, userId); } public int getUserRoleId() { Integer userID = SP(USER).getSP(ROLEID, Integer.class); return userID; } public void putRoleID(int roleId) { SP(USER).putSP(ROLEID, roleId); } public void putTrueName(String trueName) { SP(USER).putSP(TrueName, trueName); } public String getTrueName() { String TrueName1 = SP(USER).getSP(TrueName, String.class); return TrueName1; } public void putTel(String tel) { SP(USER).putSP(TEL, tel); } public String getTel() { String tel = SP(USER).getSP(TEL, String.class); return tel; } public void putAddress(String address) { SP(USER).putSP(ADDRESS, address); } public String getAddress() { String address = SP(USER).getSP(ADDRESS, String.class); return address; } public void putStorageList(String storageList){ SP(USER).putSP(STORAGE,storageList); } public String getStorageList(){ return SP(USER).getSP(STORAGE,String.class); } public void putCustomerList(String customerList){ SP(USER).putSP(CUSTOMER,customerList); } public String getCustomerList(){ return SP(USER).getSP(CUSTOMER,String.class); } private static final String SERVER_URL = "SERVER_URL";//服务器地址 private static final String SERVER_PORT = "SERVER_PORT";//服务器端口 public String getServerUrl() { return SP(USER).getSP(SERVER_URL, String.class); } public void setServerUrl(String serverUrl) { SP(USER).putSP(SERVER_URL, serverUrl); } public String getServerPort() { return SP(USER).getSP(SERVER_PORT, String.class); } public void setServerPort(String serverPort) { SP(USER).putSP(SERVER_PORT, serverPort); } }
android SharedPreferences 指定文件 名存储,获取
最新推荐文章于 2024-08-09 22:40:08 发布