======================================================================================
public class Param
{
public void setSharedPreferences(String columns, String data, Context ctxs)
{
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctxs);
SharedPreferences.Editor editor = settings.edit();
editor.putString(columns, data);
editor.commit();
}
public String getSharedPreferences(String columns, Context ctxs)
{
String param="no";
try{
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ctxs);
param=sp.getString(columns, param);
}catch(Exception ex){}
return param;
}
}
==============================extends Fragment==============================
Activity mActivity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = (Activity) activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
Param pm = new Param();
pm.setSharedPreferences("startDay", "20150402", mActivity);
String startDay = pm.getSharedPreferences("startDay", mActivity);
}
==============================extends Activity==============================
@Override
public void onCreate(Bundle savedInstanceState) {
Param pm = new Param();
pm.setSharedPreferences("startDay", "20150402", this);
String startDay = pm.getSharedPreferences("startDay", this);
}
沒有留言:
張貼留言