保存數據到SharedPreference

創建一個登錄界面,輸入賬號密碼,點擊登錄後保存賬號密碼到SharedPreference(如果勾選了保存密碼複選框),再次啓動讀取文件信息將賬號密碼自動顯示出來

數據操作代碼:

package com.suzi.qqlogin.utils;

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

import java.util.HashMap;
import java.util.Map;

/**
* Created by Suzi on 2016/7/10.
*/
public class InfoUtilOfSharedPreference
{
    /**
    * 保存數據
    *
    * @param number
    * @param password
    * @return
    */
    public static boolean SaveInfo(Context context, String number, String password)
    {

        try
        {
            SharedPreferences sp = context.getSharedPreferences("shared_prefs_info", Context
                    .MODE_PRIVATE);
            Editor editor = sp.edit();
            editor.putString("number", number);
            editor.putString("password", password);
            editor.commit();
            return true;
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    /**
    * 讀取數據
    *
    * @return
    */
    public static Map<String, String> GetInfo(Context context)
    {
        try
        {
            SharedPreferences sp = context.getSharedPreferences("shared_prefs_info", Context
                    .MODE_PRIVATE);
            String number = sp.getString("number", null);
            String password = sp.getString("password", null);

            Map<String, String> infoMap = new HashMap<String, String>();
            infoMap.put("number", number);
            infoMap.put("password", password);
            return infoMap;
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章