SharedPreferences
Cordova Plugin for Android SharedPreferences
in.edelworks.sharedpreferences
Author: R. Pramod Nair
edelworks.in
Organization: EdelWorksPlatform Support
Android
Installation
$> cd project-path
https://github.com/edelworksgithub/SharedPreferences.git
$> cordova plugin addUninstallation
$> cd project-path
$> cordova plugin remove in.edelworks.sharedpreferences
Reference for Shared Preferences in Android
Android Developer - Saving Key-Value Sets
How to
Initialize shared Preference file
sharedpreferences.getSharedPreferences(filename, mode, successHandler, errorHandler);
filename - SharedPrefernces File Name
mode - mode of Preference file. two accepted (MODE_APPEND, MODE_PRIVATE)
Save String value into Preference file
sharedpreferences.putString(key, value, successHandler, errorHandler);
Save Integer value into Preference file
sharedpreferences.putInt(key, value, successHandler, errorHandler);
Save Long value into Preference file
sharedpreferences.putLong(key, value, successHandler, errorHandler);
Save Boolean value into Preference file
sharedpreferences.putBoolean(key, value, successHandler, errorHandler);
Get String value from Preference file
sharedpreferences.getString(key, value, successHandler, errorHandler);
Get Integer value from Preference file
sharedpreferences.getInt(key, value, successHandler, errorHandler);
Get Long value from Preference file
sharedpreferences.getLong(key, value, successHandler, errorHandler);
NOTE: returned value will be in String type. Please use eval() or parseInt() for calculation use
Get Boolean value from Preference file
sharedpreferences.getBoolean(key, value, successHandler, errorHandler);
NOTE: returned value will be 0 - for false and 1 - true
Newly Added (Thanks to @octera)
Save Float value into Preference file
sharedpreferences.putFloat(key, value, successHandler, errorHandler);
Get Float value from Preference file
sharedpreferences.getFloat(key, value, successHandler, errorHandler);
Examples
Initialize shared Preference file
function createPref(filename, mode){
sharedpreferences.getSharedPreferences(filename, mode, successHandler, errorHandler);
}
function successHandler(result){
alert("SUCCESS: \r\n"+result );
}
function errorHandler(result){
alert("ERORR: \r\n"+result );
}
Put Values
function putValue(key, value, type){
switch(type){
case "string":
sharedpreferences.putString(key, value, successHandler, errorHandler);
break;
case "int":
sharedpreferences.putInt(key, value, successHandler, errorHandler);
break;
case "long":
sharedpreferences.putLong(key, value, successHandler, errorHandler);
break;
case "boolean":
sharedpreferences.putBoolean(key, value, successHandler, errorHandler);
break;
}
}
Get Values
function getValue(key, type){
switch(type){
case "string":
sharedpreferences.getString(key, successHandler, errorHandler);
break;
case "int":
sharedpreferences.getInt(key, successHandler, errorHandler);
break;
case "long":
sharedpreferences.getLong(key, successHandler, errorHandler);
break;
case "boolean":
sharedpreferences.getBoolean(key, successHandler, errorHandler);
break;
}
}
Remove a Key from Preference file
function remove(key){
sharedpreferences.remove(key, successHandler, errorHandler);
}
Clear a preference file
function clearAll(){
sharedpreferences.clear(successHandler, errorHandler);
}