SharedPreferences

2016-10-22  本文已影响0人  pengtuanyuan
package com.example.pengtuanyuan.logindemo01;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

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


public class UserInfoUtil {
    public static Boolean saveUserInfo_android (Context context,String useName, String usePassword) {


        try {

            //SharedPreferences sharedPreferences = context.getSharedPreferences("user_info", Context.MODE_PRIVATE);
            SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString("userName",useName);
            editor.putString("usePassword",usePassword);
            editor.commit();

            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
    public static Map<String,String> getUserInfo_android(Context context){

        try {
            //SharedPreferences sharedPreferences = context.getSharedPreferences("user_info", Context.MODE_PRIVATE);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            String username = sharedPreferences.getString("username", "");
            String password = sharedPreferences.getString("password", "");


            HashMap<String,String> hashMap=new HashMap<String,String>();
            hashMap.put("useName",username);
            hashMap.put("usePassword",password);

            return hashMap;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;


    }
}

上一篇 下一篇

猜你喜欢

热点阅读