Android應用如何儲存本機資料

來源:互聯網
上載者:User

標籤:

安卓應用將資料存放區在本地的方式一共有四種,按照輕量級到重量級的順序分別是:

1、用SharedPreferences儲存資料

2、用ContentProvider儲存資料

3、用SQLite儲存資料

4、用本地檔案儲存體資料

如果加上用網路儲存資料,一共就有五種了,首先我們來看第一種SharedPreference:

一、interface android.content.SharedPreferences

它的屬性類似於早期windows的ini設定檔,最終是以xml方式來儲存,你也可以理解為一個小型的key-value資料庫。

SharedPreference本質上是一個介面,它是通過調用Dalvik底層XMLParser處理XML的,資源佔用貌似不是特別大。讓我們來看看android SDK的文檔裡怎麼說:

Interface for accessing and modifying preference data returned by Context.getSharedPreferences. For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage. Objects that are returned from the various get methods must be treated as immutable by the application. 
Note: currently this class does not support use across multiple processes. This will be added later.

翻譯一下:

它是一個用於訪問和更改由 Context.getSharedPrenference返回的偏好(或喜好設定、屬性、配置,隨便你怎麼說,)的介面。對於任意一組特定的屬性,有一個所有用戶端共用的單例。如果你想更改這些屬性,你必須通過一個Editor(SharedPreferences的內部類)對象來保證屬性值在被提交到存放裝置的時候,他們能保持在一致和受控的狀態。程式必須以immutable(不可變)方式處理以get方法返回的對象。
備忘:這個類當前不支援跨進程訪問,這個特性以後會增加。

使用SharedPreferences的四個步驟:

1、得到Context對象

2、用Context對象的getSharedPreferences方法得到一個SharedPreference對象,下面是android SDK 文檔:

public abstract SharedPreferences getSharedPreferences (String name, int mode)

Retrieve and hold the contents of the preferences file ‘name‘, returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other‘s edits as soon as they are made.

Parameters
name Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
mode Operating mode. Use 0 or MODE_PRIVATE for the default operation,MODE_WORLD_READABLE andMODE_WORLD_WRITEABLE to control permissions.
Returns
  • The single SharedPreferences instance that can be used to retrieve and modify the preference values.

3、用SharedPreference對象的edit()方法得到一個Editor對象(如果不需要寫,這一步可以省略)

4、得到某一個具體的preference屬性值

5、用editor.putXXX()方法修改屬性值,XXX是類型名(如Long、Int、String等)(如果不需要寫,這一步可以省略)

6、用editor.commit()方法提交更改(如果不需要寫,這一步可以省略)


下面是範例程式碼:

import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;/** * @author Chandlerpublic class TestPref extends Activity {SharedPreferences mySharedPreferences;Editor editor;long oldvalue,newvalue = 1234;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_interval_compare);//得到Context對象Context ctx = TestPref.this;//得到SharedPreference對象,test是屬性檔案名稱mySharedPreferences = ctx.getSharedPreferences("test", MODE_PRIVATE);//得到editor對象editor = mySharedPreferences.edit();//讀取檔案中儲存的老值,如果不存在,返回-1oldValue = mySharedPreferences.getLong("oldValue",-1);//在Logcat中輸出讀取的oldValue值System.out.println(oldValue);}//退出時儲存@Overrideprotected void onDestroy(){super.onDestroy();//將long newValue存放到oldValue中去editor.putLong("oldValue", newValue);//提交更改editor.commit();Log.d(TAG, "----onDestroy----");}}

上述代碼還未debug,可以自己跑跑看,剩下的有空再更

Android應用如何儲存本機資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.