public static final String PREFS_NAME = "com.examples.myapplication.PREFS"; public static final String KEY_COUNT = "notificationCount"; private int currentCount; public void onReceive(Context context, Intent intent) { SharedPreferences values = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE); currentCount = values.getInt(KEY_COUNT, 0); //Sets to zero if not in prefs yet //Do your magic work here //Write the value back to storage for later use SharedPreferences.Editor editor = values.edit(); editor.put(KEY_COUNT,currentCount); editor.commit(); }
SharedPreferences mPreferences = PreferenceManager .getDefaultSharedPreferences(mCtx); SharedPreferences.Editor mEditor = mPreferences.edit(); mEditor.put("keyOfTheSetting",value); mEditor.commit();
public class mActivity extends Activity { @Overrride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.id.layout); // Get current version of the app PackageInfo packageInfo = this.getPackageManager() .getPackageInfo(getPackageName(), 0); int version = packageInfo.versionCode; SharedPreferences sharedPreferences = this.getPreferences(MODE_PRIVATE); boolean shown = sharedPreferences.getBoolean("shown_" + version, false); ImageView imageView = (ImageView) this.findViewById(R.id.newFeature); if(!shown) { imageView.setVisibility(View.VISIBLE); // "New feature" has been shown, then store the value in preferences SharedPreferences.Editor editor = sharedPreferences.edit(); editor.put("shown_" + version, true); editor.commit(); } else imageView.setVisibility(View.GONE); }
@Override protected void onPause() { // Save scroll position SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0); SharedPreferences.Editor editor = preferences.edit(); int scroll = mListView.getScrollY(); editor.put("ScrollValue", scroll); editor.commit(); } @Override protected void onResume() { // Get the scroll position SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0); int scroll = preferences.getInt("ScrollView", 0); mListView.scrollTo(0, scroll); }
if(checkBox.isChecked()){ boolean check=true; clicked_position=getAdapterPosition(); i=new Integer(clicked_position); hashMap.put(i, check);} if(hashMap!=null){ SharedPreferences selectedplaces=context.getSharedPreferences("selectedplaces",Context.MODE_PRIVATE); SharedPreferences.Editor editor=selectedplaces.edit(); editor.put("key",hashMap.toString()); editor.commit; }
@Override public void onStop(){//Where you wish to insert data SharedPreferences data=getSharedPreferences(PREFS_FILE,0); SharedPreferences.Editor editor= count.edit(); editor.put("data","DATA"); editor.apply(); super.onStop(); }
public void SaveText(View view) { String saved = text.getText().toString(); SharedPreferences.Editor editor=prefs.edit(); editor.put("value",saved); editor.commit(); Toast.makeText(getApplicationContext(), R.string.addedfavs, Toast.LENGTH_SHORT).show(); }