Reputation: 683
I have an android app that at oncreate(), load a json setting file from a server, json file that have google analytics id, admob code, etc. Then app run.
Here is my current function how looks like
private void makeJsonObjectRequest() {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject settings = response.getJSONObject("settings");
analyticid = settings.getString("google_anaytics");
admonbannerid = settings.getString("admob_banner");
admobinterstitialid = settings.getString("admob_interstitial");
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// Adding request to request queue
addToRequestQueue(jsonObjReq);
}
But to optimize that, I want to create a local setting file in user device, when I store these settings, and instead of calling json each time, I check first this file, to get settings, if empty, then I generate new file.
Any one know a solution for that or it's possible?
Much appreciate and thank you very much in advance
Upvotes: 1
Views: 5628
Reputation: 350
You could use SharedPreferences to store the settings in the device.
From androidhive:
Initialize:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
Store data:
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
editor.commit(); // commit changes
Retrieve data:
// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
Clear/Delete one key/value:
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
Clear/Delete all data:
editor.clear();
editor.commit(); // commit changes
You can also create a custom class to manage all the preferences and call it from everyplace you need. It would be something like:
public class SharedPreferencesManager {
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "my_preferences";
//Preferences keys
public static final String MY_KEY_ONE = "myKeyOne";
// Constructor
public SharedPreferencesManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
//STORE VALUE
public void storeValue(String key, String value){ //value can be of any type you need
//Save value
editor.putString(key, value);
// DONT FORGET to commit changes
editor.commit();
}
//GET VALUE
public String getStringValue(String key){
return pref.getString(key, null);
}
//REMOVE ONE VALUE
public void removeValue(String key){
editor.remove(key);
editor.commit();
}
//CLEAR DATA
public void clearAllValues(){
editor.clear();
editor.commit();
}
}
And use it like this:
//init
preferencesManager = new SharedPreferencesManager(getApplicationContext());
//Store Value
preferencesManager.storeValue(SharedPreferencesManager.MY_KEY_ONE, "superValue");
//Get value
String myKeyValue = preferencesManager.getStringValue(SharedPreferencesManager.MY_KEY_ONE);
//Check if value exists
if(myKeyValue == null){
//loadFromJSON
}
//Clear all values
preferencesManager.clearAllValues();
Upvotes: 6
Reputation: 1200
why you don't store the settings as part of the local settings from the app you can use
SharedPreferences mPreferences = PreferenceManager
.getDefaultSharedPreferences(mCtx);
SharedPreferences.Editor mEditor = mPreferences.edit();
mEditor.put("keyOfTheSetting",value);
mEditor.commit();
and to lookup if you have the settings
mPreferences.get("keyOfTheSetting","");
Upvotes: 0