Android資料儲存五種方式總結

2021-09-11 07:12:18 字數 1868 閱讀 3312

本文介紹android平台進行資料儲存的五大方式,分別如下:   

1 使用sharedpreferences儲存資料

2 檔案儲存資料      

3 sqlite資料庫儲存資料

4 使用contentprovider儲存資料

5 網路儲存資料

sharedpreferencese的使用:

適用範圍:儲存少量的資料,且這些資料的格式非常簡單:字串型、基本型別的值。比如應用程式的各種配置資訊(如是否開啟音效、是否使用震動效果、小遊戲的玩家積分等),解鎖口 令密碼等。

核心原理:儲存基於xml檔案儲存的key-value鍵值對資料,通常用來儲存一些簡單的配置資訊。通過ddms的file explorer面板,展開檔案瀏覽樹,很明顯sharedpreferences資料總是儲存在/data/data//shared_prefs目錄下。sharedpreferences物件本身只能獲取資料而不支援儲存和修改,儲存修改是通過sharedpreferences.edit()獲取的內部介面editor物件實現。 sharedpreferences本身是一 個介面,程式無法直接建立sharedpreferences例項,只能通過context提供的getsharedpreferences(string name, int mode)方法來獲取sharedpreferences例項

class viewocl implements view.onclicklistener
核心原理:context提供了兩個方法來開啟資料檔案裡的檔案io流 fileinputstream openfileinput(string name); fileoutputstream(string name , int mode),這兩個方法第乙個引數 用於指定檔名,第二個引數指定開啟檔案的模式。

具體有以下值可選

public string read() 

instream.close();

return sb.tostring();

} catch (exception e)

return null;

}public void write(string msg) catch (exception e)

}

案例**:

// 檔案寫操作函式

private void write(string content)

printstream out = null; // 列印流物件用於輸出

try catch (exception e) finally

}} else

}// 檔案讀操作函式

private string read()

scanner scan = null; // 掃瞄輸入

stringbuilder sb = new stringbuilder();

try

return sb.tostring();

} catch (exception e) finally

}} else

return null;

}

sqlite是輕量級嵌入式資料庫引擎,它支援 sql 語言,並且只利用很少的記憶體就有很好的效能。現在的主流移動裝置像android、iphone等都使用sqlite作為複雜資料的儲存引擎,在我們為移動裝置開發應用程式時,也許就要使用到sqlite來儲存我們大量的資料,所以我們就需要掌握移動裝置上的sqlite開發技巧

android網路程式設計:

Android五種布局方式

android五種布局方式 linearlayout relativelayout tablelayout.android使用xml宣告介面布局 將程式的表現層和控制層分離 修改使用者介面時,無需更改程式的源 視覺化工具設計使用者介面 android五種布局方式 linearlayout線性布局 a...

Android五種布局方式

1 framelayout 框架布局 所有控軟只能羅列到左上角,不能進行複雜的布局 2 linelayout線性布局 可以設定它的垂直或水平屬性值,來排列所有的了元素,所有的的元素都被堆放在其它元素之後。垂直列表的每一行只會有乙個元素,而水平列表只有乙個行高。線生布局可能進行複雜的布局編排。可以巢狀...

Android資料儲存方式

1.檔案儲存,2.sd卡儲存 外部儲存 3.sp儲存 配置檔案儲存 4.資料庫儲存 儲存大量結構相似的資料,可以進行增刪改查 5.網路儲存 手機記憶體不足時存放在伺服器端的 sqlite資料庫的建立與增刪改查 1.首先建立mydbopenhelper繼承sqliteopenhelper重寫它的構造方...