使用SharedPreference保存一些简单的信息
package com.example.testsharedpreference; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; public class MyPreference { private Context context; public MyPreference(Context context) { this.context = context; } /** * 保存信息 (保存位置:“/data/data/应用程序包/shared_prefs”) * @param name * @param pswd * @return */ public boolean saveMessage(String name, String pswd) { boolean flag = false; SharedPreferences sharedPreferences = context.getSharedPreferences( "userinfo", Context.MODE_PRIVATE); //对数据进行编辑 SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putString("name", name); edit.putString("pswd", pswd); //将数据持久化到存储介质当中 edit.commit(); return flag; } /** * @return 以map形式返回一些存储的信息 */ public Map<String, String> getMessage() { Map<String, String> map = new HashMap<String, String>(); SharedPreferences sharedPreferences = context.getSharedPreferences( "userinfo", Context.MODE_PRIVATE); String name = sharedPreferences.getString("name", ""); String pswd = sharedPreferences.getString("pswd", ""); map.put("name", name); map.put("pswd", pswd); return map; } }
奋斗和第三方