SharedPreferences臨時存儲數據 如記住密碼

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

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

public class SystemSharedpreferences {
    
    private Context context;
    
    public SystemSharedpreferences(Context context) {
        this.context = context;
    }
    //存數據
    public boolean saveMessage(String name,String age){
        boolean flage = false;
        SharedPreferences sharedpreferences = context.getSharedPreferences("IPConfig",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString("name", name);
        editor.putString("age", age);
        flage = editor.commit();
        return flage;
    }
    //取數據
    public Map<String,String> getMessage(){
        Map<String,String> map = new HashMap<String,String>();
        SharedPreferences sharedPreferences = context.getSharedPreferences("IPConfig", Context.MODE_PRIVATE);
        String ip = sharedPreferences.getString("name", "");
        String port = sharedPreferences.getString("age", "");
        map.put("name",name);
        map.put("age", age);
        return map;
    }
}

//調用
//存入
SystemSharedpreferences systemSharedpreferences = new SystemSharedpreferences(
                                getBaseContext());
boolean flage = systemSharedpreferences.saveMessage(
                                name, age);
  //取出                              
 SystemSharedpreferences systemSharedpreferences = new SystemSharedpreferences(
                        getBaseContext());
 Map<String, String> maps = systemSharedpreferences.getMessage();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章