Skip to content

VenomVendor/SimpleSharedPreferences

Repository files navigation

Android SimpleSharedPreferences

Use SimpleSharedPreferences to avoid unnecessary code while writing and fetching from SharedPreferences

Build Status

Usage

Import any one *.jar into /libs

SimpleSharedPreferences.initialize(this); // from Application.

SimpleSharedPreferences mPreferences = SimpleSharedPreferences.getInstance();
mPreferences.putString("STRING_KEY", "STRING_VALUE");  // Put String
mPreferences.putInt("INTEGER_KEY", 50);  // Put Int
mPreferences.getString("STRING_KEY", "STRING_DEF_VALUE"); // Get String

##Error Throws ClassCastException when wrong key is passed

mPreferences.getString("INTEGER_KEY", "STRING_DEF_VALUE"); // Get String with Integer Key
Error:
==========================================================
ClassCastException : INTEGER_KEY's value is not a string
========================================================== 

##Demo SimpleSharedPreferencesDemo.Java
Sample.apk

Other APIs

public int getAppOpenedCount() // Get the number of times app opened

public boolean isLogEnabled() // default is false

public void enableLog(boolean enableLog) // Update Log Status

###Note

  • Can be used beside SharedPreferences without any conflict.
  • All Methods in SharedPreferences & SharedPreferences.Editor are available in SimpleSharedPreferences.

Why?

  • Was bored of using SharedPreferences.edit() and Editor.commit() every time to push data into SharedPreferences.
  • putStringSet / getStringSet can be used pre API level 11.
  • Throws Exact Exception when Wrong Key is passed.
  • Includes API to get the number of times App was opened.
  • To set "STRING_VALUE" & 50(INT_VALUE) Seperately in SharedPreferences.
// Old boring code
SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor mEditor = mPreferences.edit();
mEditor.putString("STRING_KEY", "STRING_VALUE"); // mEditor
mEditor.commit(); // mEditor
...
...
...
mEditor.putInt("INTEGER_KEY", 50); // mEditor
mEditor.commit(); // mEditor
...
mPreferences.getString("STRING_KEY", "STRING_DEF_VALUE");  // mPreferences
...

imports

import com.venomvendor.library.SimpleSharedPreferences;

ProGuard

-keepclassmembers class * implements android.content.SharedPreferences.** { *; }
Author : VenomVendor

#License Copyright (C) 2016 VenomVendor info@VenomVendor.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.