public static void setSharedKey(String key, String value, Context context) {
SharedPreferences spre = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = spre.edit();
prefEditor.putString(key, value);
//prefEditor.clear();
prefEditor.commit();
}
public static String getSharedKey(String key, Context context) {
SharedPreferences spre = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE);
return spre.getString(key, "");
}
Declare this above two method in your common class
to set shared preference
Utility.setSharedKey("key", "value", mContext);
to get shared preference
String mypref = Utility.getSharedKey("key", MainActivity.this);
SharedPreferences spre = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = spre.edit();
prefEditor.putString(key, value);
//prefEditor.clear();
prefEditor.commit();
}
public static String getSharedKey(String key, Context context) {
SharedPreferences spre = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE);
return spre.getString(key, "");
}
Declare this above two method in your common class
to set shared preference
Utility.setSharedKey("key", "value", mContext);
to get shared preference
String mypref = Utility.getSharedKey("key", MainActivity.this);
No comments:
Post a Comment
Comments