我有一个Android应用程序在oncreate(),从服务器加载json设置文件,json文件有谷歌分析ID,admob代码等。然后app运行。
这是我当前的功能如何看起来像
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);
}
但是要优化它,我想在用户设备中创建一个本地设置文件,当我存储这些设置,而不是每次调用json时,我首先检查这个文件,获取设置,如果为空,那么我生成新文件。
任何人都知道解决方案或者可能吗?
非常感谢并提前非常感谢
答案 0 :(得分:4)
您可以使用SharedPreferences将设置存储在设备中。
来自androidhive:
初始化:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
存储数据:
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
检索数据:
// 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
清除/删除一个键/值:
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
清除/删除所有数据:
editor.clear();
editor.commit(); // commit changes
您还可以创建自定义类来管理所有首选项,并从您需要的每个位置调用它。它会是这样的:
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();
}
}
并像这样使用它:
//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();
答案 1 :(得分:0)
为什么您不能将设置存储为可以使用的应用程序的本地设置的一部分
SharedPreferences mPreferences = PreferenceManager
.getDefaultSharedPreferences(mCtx);
SharedPreferences.Editor mEditor = mPreferences.edit();
mEditor.put("keyOfTheSetting",value);
mEditor.commit();
并查找是否有设置
mPreferences.get("keyOfTheSetting","");