android读取网络配置文件,Android 中保存、读取设置配置数据、记录登录状态
記錄登錄狀態,我認為可以在用戶登錄成功后,與服務器約定一個 token?憑據,保存這個 token?憑據,而不是直接保存用戶名、密碼。
當然,保存 token?與保存設置數據是一樣的,本文只講解如何保存、讀取。protected void save(View v)
{
SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", ((EditText)findViewById(R.id.username)).getText().toString());
editor.putString("password", ((EditText)findViewById(R.id.password)).getText().toString());
// editor.putBoolean()、editor.putInt()、editor.putFloat()……
editor.commit();
}
protected void load(View v)
{
SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
String username = sp.getString("username", ""); // 第二個參數為默認值
String password = sp.getString("password", ""); // 第二個參數為默認值
// sp.getBoolean()、sp.getInt()、sp.getFloat()……
((EditText)findViewById(R.id.username)).setText(username);
((EditText)findViewById(R.id.password)).setText(password);
}
config?為本配置文件的名稱,Context.MODE_PRIVATE?表示只能被本應用讀寫,Activity.MODE_WORLD_READABLE?表示可被其他應用讀,Activity.MODE_WORLD_WRITEABLE?表示可被其他應用寫。
讀是直接調用的 SharedPreferences?方法,寫是調用的 SharedPreferences.Editor?方法。
相關閱讀
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的android读取网络配置文件,Android 中保存、读取设置配置数据、记录登录状态的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 诺基亚7原生android,【IT之家出
- 下一篇: android+动画队列,Android