詳解Android四種存儲方式
在Android程序開發(fā)中我們經(jīng)常遇到四種數(shù)據(jù)存儲方式,每種存儲方式都各有不同;以下我分別列舉了Android開發(fā)中的不同存儲方式的特點
一,Preferences
Preferences是一個較輕量級的存儲數(shù)據(jù)的方法,具體使用方法:
在A中保存值:
SharedPreferences.Editor sharedata = getSharedPreferences("data", 0).edit();
sharedata.putString("name","shenrenkui");
sharedata.commit();
在B中取值:
SharedPreferences sharedata = getSharedPreferences("data", 0);
String data = sharedata.getString("name", null);
Log.i(TAG,"data="+data);
注 意,Context.getSharedPreferences(String name,int type)的參數(shù)更我們在創(chuàng)建數(shù)據(jù)的時候的數(shù)據(jù)權(quán)限屬性是一樣的,存儲和取值的過程這有點像HashMap但是比HashMap更具人性 化,getXXX(Object key,Object defualtReturnValue),第二個參數(shù)是當(dāng)你所要的key對應(yīng)沒有時候返回的值。這就省去了很多邏輯判斷。。。。
二,F(xiàn)iles
在Android上面沒有的File就是J2se中的純種File了,可見功能之強大,這里就算是走馬觀花地嚴重路過了。
//創(chuàng)建文件
file = new File(FILE_PATH , FILE_NAME);
file.createNewFile();
//打開文件file的OutputStream
out = new FileOutputStream(file);
String infoToWrite = "紙上得來終覺淺,絕知此事要躬行";
//將字符串轉(zhuǎn)換成byte數(shù)組寫入文件
out.write(infoToWrite.getBytes());
//關(guān)閉文件file的OutputStream
out.close();
//打開文件file的InputStream
in = new FileInputStream(file);
//將文件內(nèi)容全部讀入到byte數(shù)組
int length = (int)file.length();
byte[] temp = new byte[length];
in.read(temp, 0, length);
//將byte數(shù)組用UTF-8編碼并存入display字符串中
display = EncodingUtils.getString(temp,TEXT_ENCODING);
//關(guān)閉文件file的InputStream
in.close();
} catch (IOException e) {
//將出錯信息打印到Logcat
Log.e(TAG, e.toString());
this.finish();
}
//從資源讀取
InputStream is=getResources().getRawResource(R.raw.文件名)
三,Databases
Android 內(nèi)嵌了功能比其他手機操作系統(tǒng)強大的關(guān)系型數(shù)據(jù)庫sqlite3,我們在大學(xué)時候?qū)W的SQL語句基本都可以使用,我們自己創(chuàng)建的數(shù)據(jù)可以用adb shell來操作。具體路徑是/data/data/package_name/databases。如,這里演示一下進入 com.android.providers.media包下面的操作。
1, adb shell
2, cd /data/data/com.android.providers.media/databases
3, ls(查看com.android.providers.media下面的數(shù)據(jù)庫)
4, sqlite3 internal.db
5, .help---看看如何操作
6, .table列出internal數(shù)據(jù)中的表
7, select * from albums;
DatabaseHelper mOpenHelper;
private static final String DATABASE_NAME = "dbForTest.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "diary";
private static final String TITLE = "title";
private static final String BODY = "body";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ");";
Log.i("haiyang:createDB=", sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
/**
* 重新建立數(shù)據(jù)表
*/
private void CreateTable() {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ");";
Log.i("haiyang:createDB=", sql);
try {
db.execSQL("DROP TABLE IF EXISTS diary");
db.execSQL(sql);
setTitle("數(shù)據(jù)表成功重建");
} catch (SQLException e) {
setTitle("數(shù)據(jù)表重建錯誤");
}
}
/**
* 刪除數(shù)據(jù)表
*/
private void dropTable() {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "drop table " + TABLE_NAME;
try {
db.execSQL(sql);
setTitle("數(shù)據(jù)表成功刪除:" + sql);
} catch (SQLException e) {
setTitle("數(shù)據(jù)表刪除錯誤");
}
}
/**
* 插入兩條數(shù)據(jù)
*/
private void insertItem() {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql1 = "insert into " + TABLE_NAME + " (" + TITLE + ", " + BODY
+ ") values('haiyang', 'android的發(fā)展真是迅速啊');";
String sql2 = "insert into " + TABLE_NAME + " (" + TITLE + ", " + BODY
+ ") values('icesky', 'android的發(fā)展真是迅速啊');";
try {
Log.i("haiyang:sql1=", sql1);
Log.i("haiyang:sql2=", sql2);
db.execSQL(sql1);
db.execSQL(sql2);
setTitle("插入兩條數(shù)據(jù)成功");
} catch (SQLException e) {
setTitle("插入兩條數(shù)據(jù)失敗");
}
}
/**
* 刪除其中的一條數(shù)據(jù)
*/
private void deleteItem() {
try {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
db.delete(TABLE_NAME, " title = 'haiyang'", null);
setTitle("刪除title為haiyang的一條記錄");
} catch (SQLException e) {
}
}
/**
* 在屏幕的title區(qū)域顯示當(dāng)前數(shù)據(jù)表當(dāng)中的數(shù)據(jù)的條數(shù)。
*/
private void showItems() {
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
String col[] = { TITLE, BODY };
Cursor cur = db.query(TABLE_NAME, col, null, null, null, null, null);
Integer num = cur.getCount();
setTitle(Integer.toString(num) + " 條記錄");
}
四,Network
這是借助Internet來存儲我們要的數(shù)據(jù),這是CS結(jié)構(gòu)的存儲方式,也是點一下名了。
如何使用 Content Provider
下邊是用戶經(jīng)常接觸到的幾個典型Content Provider應(yīng)用:
* Content Provider Name : Intended Data * Browser : Browser bookmarks, Browser history, etc. * CallLog : Missed calls, Call datails, etc. * Contacts : Contact details * MediaStore : Media files such as audio, Video and Images * Settings : Device Settings and Preferences
調(diào)用Content Provider資源的標準URI結(jié)構(gòu):
<standard_prefix>://<authority>/<data_path>/<id>
例如:
1) 取得瀏覽器所有“書簽”信息: content://browser/bookmarks
2) 取得系統(tǒng)通訊錄中的信息: content://contacts/people (如果取得某一個特定通訊記錄,在路徑URI的末端指定一個ID號:content://contacts/people/5
簡單的實例片段:
Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);
以上內(nèi)容是小編給大家介紹的Android四種存儲方式,希望大家喜歡,更多信息請登錄腳本之家網(wǎng)站了解更多。
- Android sdcard實現(xiàn)圖片存儲 、聯(lián)網(wǎng)下載
- Android持久化技術(shù)之SharedPreferences存儲實例詳解
- 詳解Android文件存儲
- 實例詳解Android文件存儲數(shù)據(jù)方式
- Android開發(fā)筆記之Android中數(shù)據(jù)的存儲方式(一)
- Android開發(fā)筆記之Android中數(shù)據(jù)的存儲方式(二)
- Android數(shù)據(jù)存儲之SQLite使用
- Android編程使用內(nèi)容提供者方式(ContentProvider)進行存儲的方法
- Android編程中的5種數(shù)據(jù)存儲方式
- Android使用文件進行數(shù)據(jù)存儲的方法
- Android編程之SharedPreferences文件存儲操作實例分析
- android中使用SharedPreferences進行數(shù)據(jù)存儲的操作方法
- Android應(yīng)用開發(fā)SharedPreferences存儲數(shù)據(jù)的使用方法
- 簡介Android應(yīng)用中sharedPreferences類存儲數(shù)據(jù)的用法
相關(guān)文章
Android使用MediaCodec將攝像頭采集的視頻編碼為h264
這篇文章主要為大家詳細介紹了Android使用MediaCodec將攝像頭采集的視頻編碼為h264,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
Android DSelectorBryant 單選滾動選擇器的實例代碼
本文通過實例代碼給大家介紹了Android DSelectorBryant 單選滾動選擇器的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
android教程之intent的action屬性使用示例(intent發(fā)短信)
這篇文章主要介紹了android中intent的action屬性使用示例,提供了使用intent撥打電話、發(fā)送短信、播放mp3的代碼2014-01-01
深入分析Android ViewStub的應(yīng)用詳解
本篇文章是對Android ViewStub的應(yīng)用進行了詳細的分析介紹,需要的朋友參考下2013-05-05
微信小程序—微信跳一跳,Android游戲助手(外掛)使用教程詳解
這篇文章主要介紹了微信小程序—微信跳一跳,Android游戲助手(外掛)使用教程詳解,需要的朋友可以參考下2018-01-01
Android打開淘寶客戶端(手淘)效果及實現(xiàn)代碼
這篇文章主要介紹了Android打開淘寶客戶端(手淘)效果及實現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04
android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode
這篇文章主要介紹了android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode,有需要的可以了解一下。2016-11-11
使用android隱藏api實現(xiàn)亮度調(diào)節(jié)的方法
使用android隱藏api實現(xiàn)亮度調(diào)節(jié)的方法,需要的朋友可以參考一下2013-05-05

