Android開發:SharedPreferences 儲存資料、擷取資料

來源:互聯網
上載者:User

標籤:val   travel   user   擷取資料   sha   activity   art   article   div   

Android開發:SharedPreferences 儲存資料、擷取資料

email:[email protected]

      開發環境:win7 64位,Android Studio。

      關於SharedPreferences。個人理解把它理解為一種輕量級的Database。存取形式和map一樣:<key,value>,以xml檔案儲存體。

我僅僅是用它來儲存一下登入資訊和登入狀態,這樣每次登入的時候就能夠從本地讀取資訊。

一.儲存

/**                             * 儲存個人資訊在本地                             */                            SharedPreferences sharedPreferences = getSharedPreferences("intVeh", Context.MODE_PRIVATE);                            SharedPreferences.Editor editor = sharedPreferences.edit();//擷取編輯器                            //還應該記錄一下user_id                            editor.putString("userId",userId);                            editor.putString("phone", userPhone);                            editor.putString("password", userPassword);                            //記錄登入狀態                            editor.putString("status","1");                            editor.commit();//提交改動

二.讀取

 SharedPreferences sharedPreferences = getSharedPreferences("intVeh",Activity.MODE_PRIVATE);                String userId = sharedPreferences.getString("userId","");                String status = sharedPreferences.getString("status","0");

請注意:sharedPreferences.getString("key","當key不存在時返回的預設值")。

很多其它關於sharedPreferences,能夠參考:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059133.html


Android開發:SharedPreferences 儲存資料、擷取資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.