Back to project page BluetoothSppPro.
The source code is released under:
Apache License
If you think the Android project BluetoothSppPro listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package mobi.dzs.android.storage; // w ww. j a va 2s .com import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; final public class CSharedPreferences extends CKVStorage{ private static final String msDELIMITER = "|_|"; /** Android Activity???? */ private Context mc = null; /** ???? */ private String msPkgName; /** SharedPreferences?Editor?????? */ private Editor meSaveData = null; /** android????????????:/data/data/<package name>/shared_prefs */ private SharedPreferences mSP = null; /** * ???? * @param C Context Activity????? */ public CSharedPreferences(Context C){ this.mc = C; PackageManager manager = mc.getPackageManager(); PackageInfo info; try{ info = manager.getPackageInfo(mc.getPackageName(), 0); this.msPkgName = info.packageName; this.mSP = mc.getSharedPreferences(this.msPkgName, Context.MODE_PRIVATE); this._bSrorageIsReady = true; //???????? }catch (NameNotFoundException e){ e.printStackTrace(); this.msPkgName = ""; this._bSrorageIsReady = false; } } /** * ???????????<br/> * (??????????????) * */ private void newStorage(){ if (null == this.meSaveData) this.meSaveData = mc.getSharedPreferences(this.msPkgName, Context.MODE_PRIVATE).edit(); //????? } /** * ??????????? * @param sKey String ???? * @param sSubKey String ????? * @return String ???????????????? * */ private String getIdxKey(String sKey, String sSubKey){ return sKey + msDELIMITER + sSubKey; } @Override public boolean saveStorage() { if (null != this.meSaveData){ this.meSaveData.commit();//??????????? this.meSaveData = null; //??????????? return true; } else return false; } @Override public CKVStorage setVal(String sKey, String sSubKey, String sVal) { this.newStorage(); this.meSaveData.putString(this.getIdxKey(sKey, sSubKey), sVal); return this; } @Override public CKVStorage setVal(String sKey, String sSubKey, int iVal) { this.newStorage(); this.meSaveData.putInt(this.getIdxKey(sKey, sSubKey), iVal); return this; } @Override public CKVStorage setVal(String sKey, String sSubKey, double dbVal) { this.newStorage(); this.meSaveData.putFloat(this.getIdxKey(sKey, sSubKey), (float)dbVal); return this; } @Override public CKVStorage setVal(String sKey, String sSubKey, long lVal) { this.newStorage(); this.meSaveData.putLong(this.getIdxKey(sKey, sSubKey), lVal); return this; } @Override public CKVStorage setVal(String sKey, String sSubKey, boolean bVal) { this.newStorage(); this.meSaveData.putBoolean(this.getIdxKey(sKey, sSubKey), bVal); return this; } @Override public String getStringVal(String sKey, String sSubKey) { return this.mSP.getString(this.getIdxKey(sKey, sSubKey), ""); } @Override public double getDoubleVal(String sKey, String sSubKey) { return (double)this.mSP.getFloat(this.getIdxKey(sKey, sSubKey), 0.0f); } @Override public int getIntVal(String sKey, String sSubKey) { return this.mSP.getInt(this.getIdxKey(sKey, sSubKey), 0); } @Override public long getLongVal(String sKey, String sSubKey) { return this.mSP.getLong(this.getIdxKey(sKey, sSubKey), 0); } @Override public boolean getBooleanVal(String sKey, String sSubKey) { return this.mSP.getBoolean(this.getIdxKey(sKey, sSubKey), false); } @Override public CKVStorage removeVal(String sKey, String sSubKey) { this.newStorage(); this.meSaveData.remove(this.getIdxKey(sKey, sSubKey)); return this; } }