欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android四種存儲方式

 更新時間:2015年12月22日 15:01:02   投稿:mrr  
在Android程序開發(fā)中我們經(jīng)常遇到四種數(shù)據(jù)存儲方式,每種存儲方式都各不相同,下面通過本篇文章給大家介紹android四種存儲方式,對此感興趣的朋友一起學(xué)習(xí)吧

在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資源的標(biāo)準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)站了解更多。

相關(guān)文章

最新評論