SharedPreferences使用


其他代码同,QQ登录

<span style="font-size:14px;">package com.itheima28.qqlogin.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
import android.text.TextUtils;

public class UtilsOfSharePreferences {
	/**
	 * 保存用户信息到sd卡
	 * */
	public static boolean saveUserInfo(Context context,String number,String password){
		
		try {
			//    /data/data/包名/shared_prefs/ithmima28
			SharedPreferences sp = context.getSharedPreferences("ithmima28", Context.MODE_PRIVATE);
			
			//获得一个编辑对象
			Editor edit = sp.edit();
			
			//存数据
			edit.putString("number", number);
			edit.putString("password", password);
			
			//提交数据,真正存储起来了
			edit.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 *  到sd卡获取用户信息
	 * */
	 public static Map<String,String> getUserInfo(Context context){
		 
		 SharedPreferences sp = context.getSharedPreferences("ithmima28", Context.MODE_PRIVATE);
		 
		 String number = sp.getString("number", null);
		 String password = sp.getString("password", null); 
		 
		 if(!TextUtils.isEmpty(number) && !TextUtils.isEmpty(number)){
			 Map<String,String> userInfoMap = new HashMap<String, String>();
			 userInfoMap.put("number", number);
			 userInfoMap.put("password", password);
			 return userInfoMap;
		 }
		 
		 return null;
	}
}</span>


你可能感兴趣的:(android)