[工具類]Android資料存放區sharedpreferences加密

來源:互聯網
上載者:User

[java] 
import java.util.List; 
import java.util.Map; 
import android.content.Context; 
import android.content.SharedPreferences; 
 
 
/**
 * 儲存配置資訊的工具類 <br>
 * 註:可讀取的資料類型有-<code>boolean、int、float、long、String.</code>
 */ 
public class SharePreferenceUtil { 
    private final String MAK = "innoview"; 
    private final int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE; 
    private final SharedPreferences sharedpreferences; 
 
    public SharePreferenceUtil(Context context, String fileName) { 
        sharedpreferences = context.getSharedPreferences(fileName, MODE); 
    } 
 
    public boolean saveSharedPreferences(String key, String value) { 
        SharedPreferences.Editor editor = sharedpreferences.edit(); 
        try { 
            editor.putString(key, AESEncryptor.encrypt(MAK, value)); 
        } catch (Exception e) { 
            editor.putString(key, value); 
            e.printStackTrace(); 
        } 
        return editor.commit(); 
    } 
 
    public String loadStringSharedPreference(String key) { 
        String str = null; 
        try { 
            str = sharedpreferences.getString(key, null); 
            if (str != null && !"".equals(str)){ 
                str = AESEncryptor.decrypt(MAK, str); 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return str; 
    } 
 
    public boolean saveSharedPreferences(String key, int value) { 
        SharedPreferences.Editor editor = sharedpreferences.edit(); 
        editor.putInt(key, value); 
        return editor.commit(); 
    } 
 
    public int loadIntSharedPreference(String key) { 
        return sharedpreferences.getInt(key, 0); 
    } 
 
    public boolean saveSharedPreferences(String key, float value) { 
        SharedPreferences.Editor editor = sharedpreferences.edit(); 
        editor.putFloat(key, value); 
        return editor.commit(); 
    } 
 
    public float loadFloatSharedPreference(String key) { 
        return sharedpreferences.getFloat(key, 0f); 
    } 
 
    public boolean saveSharedPreferences(String key, Long value) { 
        SharedPreferences.Editor editor = sharedpreferences.edit(); 
        editor.putLong(key, value); 
        return editor.commit(); 
    } 
 
    public long loadLongSharedPreference(String key) { 
        return sharedpreferences.getLong(key, 0l); 
    } 
 
    public boolean saveSharedPreferences(String key, Boolean value) { 
        SharedPreferences.Editor editor = sharedpreferences.edit(); 
        editor.putBoolean(key, value); 
        return editor.commit(); 
    } 
 
    public boolean loadBooleanSharedPreference(String key) { 
        return sharedpreferences.getBoolean(key, false); 
    } 
 
    public boolean saveAllSharePreference(String keyName, List<?> list) { 
        int size = list.size(); 
        if (size < 1) { 
            return false; 
        } 
        SharedPreferences.Editor editor = sharedpreferences.edit(); 
        if (list.get(0) instanceof String) { 
            for (int i = 0; i < size; i++) { 
                editor.putString(keyName + i, (String) list.get(i)); 
            } 
        } else if (list.get(0) instanceof Long) { 
            for (int i = 0; i < size; i++) { 
                editor.putLong(keyName + i, (Long) list.get(i)); 
            } 
        } else if (list.get(0) instanceof Float) { 
            for (int i = 0; i < size; i++) { 
                editor.putFloat(keyName + i, (Float) list.get(i)); 
            } 
        } else if (list.get(0) instanceof Integer) { 
            for (int i = 0; i < size; i++) { 
                editor.putLong(keyName + i, (Integer) list.get(i)); 
            } 
        } else if (list.get(0) instanceof Boolean) { 
            for (int i = 0; i < size; i++) { 
                editor.putBoolean(keyName + i, (Boolean) list.get(i)); 
            } 
        } 
        return editor.commit(); 
    } 
 
    public Map<String, ?> loadAllSharePreference(String key) { 
        return sharedpreferences.getAll(); 
    } 
 
    public boolean removeKey(String key) { 
        SharedPreferences.Editor editor = sharedpreferences.edit(); 
        editor.remove(key); 
        return editor.commit(); 
    } 
 
    public boolean removeAllKey() { 
        SharedPreferences.Editor editor = sharedpreferences.edit(); 
        editor.clear(); 
        return editor.commit(); 
    } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.