美文网首页
SharedPreferences

SharedPreferences

作者: pengtuanyuan | 来源:发表于2016-10-22 22:32 被阅读0次
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;


    }
}

相关文章

网友评论

      本文标题:SharedPreferences

      本文链接:https://www.haomeiwen.com/subject/ouvhuttx.html