編輯:初級開發
android的數據存儲如果如下幾個方式:
Shared Preferences
存儲數據以key-value的形式保存在XML文件中。
Internal Storage
存儲在apk安裝目錄。
External Storage
存儲在擴展的地方,一般指SD卡。
SQLite Databases
數據庫存儲。
Network Connection
網絡存儲。
Shared Preferences
是系統本身自帶封裝好了的方式,實際上就是存儲在apk安裝所在目錄,數據的存放形式是XML,讀取和寫入的例子如下:
Java代碼
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}
利用APK本身存儲
這種存儲方式是把文具存儲在文件中,其存位置也在app安裝目錄裡,如果用戶刪除app,隨之的文件也將被刪除,主要提供兩個方法:
openFileOutput()
openFileInput()
兩個的返回類型都是FileInputStream,例子如下:
Java代碼
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
緩存文件
緩存文件也是存儲在app本身的安裝目錄裡,只是和openFileOutput不在一個目錄裡,緩存目錄裡面的數據用戶是可以用戶手動刪除的, openFileOutput就不行,getCacheDir()獲取緩存目錄,根據自己的需求讀寫文件。
擴展存儲
一般指的是SD卡的存儲,使用的示例如下:
Java代碼
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
數據庫存儲
數據庫是在WEB應用中很常用的方式,應用場合比較多,如果不熟,可以去看下官方提供的notepad例子,裡面講sqlite的操作,寫的比較清楚,這裡提供一個簡單的示例如下:
Java代碼
public class MyDbOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DICTIONARY_TABLE_NAME = "dictionary";
private static final String DICTIONARY_TABLE_CREATE =
"CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +
KEY_Word + " TEXT, " +
KEY_DEFINITION + " TEXT);";
DictionaryOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DICTIONARY_TABLE_CREATE);
}
}
public class MyDbOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DICTIONARY_TABLE_NAME = "dictionary";
private static final String DICTIONARY_TABLE_CREATE =
"CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +
KEY_Word + " TEXT, " +
KEY_DEFINITION + " TEXT);";
DictionaryOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DICTIONARY_TABLE_CREATE);
}
}
網絡存儲
一般利用網絡獲取數據或者通過網絡上傳數據,常用的類是:Java.net.*,android.Net.*
最近逛其它論壇的時候看到有這樣一個問題,需要界面加載完成後自動彈出軟鍵盤。開始我認為沒有那麼麻煩,最後自己做了一個小例子,還真不好搞定,直到昨天再想這個的問題的時候,想
在經過長時間的等待之後我們在今天終於看見了看見了全新的Google Reader軟件,當然更為難能可貴的是這款軟件竟然是Google官方推出的,目前廣大的用戶已經可以在
三、範例程式:Activity與Service間之溝通先執行ac01:這個ac01立即啟動myService,定時連續傳來數字,如下:數字連續增加下去。其程式碼為:/*
接上,其實BnMediaPlayerService->onTransact函數的結構也很簡單,就是switch...case...接收不同的請求執行不同的代碼調用