Android 存儲方式比較多,常用的有4中存儲,他們分別是
- SharedPreferences存儲方式
- 文件存儲
- 數據庫存儲
- ContentProvider存儲
SharedPreferences存儲方式
常常用來進行一些簡單的數據本地存儲,用來保存簡單的配置信息。例如用戶登錄信息,設置的信息等等。它是基於Map鍵值對來進行存儲的。但是使用SharedPreferences是有些限制的,只能在同一個包內使用,不能在不同的包之間使用。而且只能對一些簡單數據進行保存
private static SharedPreferences mSharedPreferences;
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//保存String類型信息
public static void set(String key, String value) {
Editor editor = getPreferences().edit();
editor.putString(key, value);
apply(editor);
}
//讀取String類型信息
public static String get(String key, String defValue) {
return getPreferences().getString(key, defValue);
}
文件存儲
文件存儲實際上就是對文件IO的操作。文件存儲操作的一般步驟爲:
- 創建並打開文件(如果文件存在則打開文件)
- 對打開後的文件進行讀寫操作
打開文件主要用到了openFileInput()和openFileOutput() 兩個方法,打開之後將返回FileInputStream或FileOutputStream文件流。然後對流進行讀寫操作。下面以文本文件的讀寫爲例來說明
/**
* 寫文本文件 在Android系統中,文件保存在 /data/data/PACKAGE_NAME/files 目錄下
*
* @param context
* @param msg
*/
public static void write(Context context, String fileName, String content) {
if (content == null)
content = "";
try {
FileOutputStream fos = context.openFileOutput(fileName,
Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 讀取文本文件
*
* @param context
* @param fileName
* @return
*/
public static String read(Context context, String fileName) {
try {
FileInputStream in = context.openFileInput(fileName);
return readInStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String readInStream(InputStream inStream) {
try {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
int length = -1;
while ((length = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, length);
}
outStream.close();
inStream.close();
return outStream.toString();
} catch (IOException e) {
Log.i("FileTest", e.getMessage());
}
return null;
}
數據庫存儲
Android自帶有SQLite數據庫引擎。SQLite是輕量級嵌入式數據庫引擎,它支持 SQL 語言,並且只利用很少的內存就有很好的性能。現在的主流移動設備像Android、iPhone等都使用SQLite作爲複雜數據的存儲引擎。
Android 中主要通過sqliteopenhelper類 和sqlitedatabase類,Cursor類來對數據庫進行操作。其中SQLiteOpenHelper類是一個輔助類,主要用於產生一個數據庫對象並對數據庫進行版本管理。其中的主要方法包括
- private SQLiteDatabase mDatabase;//DB
- public abstract void onCreate(SQLiteDatabase db);//創建DB
- public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);//版本更新
- public SQLiteDatabase getReadableDatabase()//對DB進行讀操作
- public SQLiteDatabase getWritableDatabase()//對DB進行寫操作
而對於數據庫本身的增刪改查操作是有SQLiteDatabase類的對象(可以看成數據庫對象)完成的。對數據庫的操作分爲兩種方式:一種是直接通過執行SQL語句來操作,
sqlite.execSQL(“SQL語句”);
sqlite.close();
舉個栗子
public void deleteWithExecSQL(int id) {
SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
String sql = ("delete from " + DatabaseHelper.FRIENDS_TABLE_NAME + " where _id=?");
sqlite.execSQL(sql, new Integer[] { id });
sqlite.close();
}
public void deleteWithDB (int id){
SQLiteDatabase sqLite = dbHelper.getWritableDatabase();
sqLite.delete(DatabaseHelper.FRIENDS_TABLE_NAME,"WHERE _id="+id,null);
}
至於SQL語法,這裏就不詳細介紹了,自己GOOGLE。
ContentProvider內容提供器
使用ContentProvider共享數據的好處是統一了數據訪問方式。就好比將上述的幾種存儲方式統一了起來,可能我需要的數據在DB中,我可以通過內容提供器來獲取數據;可能我需要的數據在SharePreference中,不嫌麻煩我也可以通過內容提供器來獲取。
這裏提供一篇CSDN博客,將的比較詳細。http://blog.csdn.net/faith_boys/article/details/8917395