android读取网络配置文件,Android 中保存、读取设置配置数据、记录登录状态
記錄登錄狀態(tài),我認(rèn)為可以在用戶登錄成功后,與服務(wù)器約定一個 token?憑據(jù),保存這個 token?憑據(jù),而不是直接保存用戶名、密碼。
當(dāng)然,保存 token?與保存設(shè)置數(shù)據(jù)是一樣的,本文只講解如何保存、讀取。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", ""); // 第二個參數(shù)為默認(rèn)值
String password = sp.getString("password", ""); // 第二個參數(shù)為默認(rèn)值
// sp.getBoolean()、sp.getInt()、sp.getFloat()……
((EditText)findViewById(R.id.username)).setText(username);
((EditText)findViewById(R.id.password)).setText(password);
}
config?為本配置文件的名稱,Context.MODE_PRIVATE?表示只能被本應(yīng)用讀寫,Activity.MODE_WORLD_READABLE?表示可被其他應(yīng)用讀,Activity.MODE_WORLD_WRITEABLE?表示可被其他應(yīng)用寫。
讀是直接調(diào)用的 SharedPreferences?方法,寫是調(diào)用的 SharedPreferences.Editor?方法。
相關(guān)閱讀
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的android读取网络配置文件,Android 中保存、读取设置配置数据、记录登录状态的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 诺基亚7原生android,【IT之家出
- 下一篇: html5在线考试开发,基于HTML5的