Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android之數據存儲

Android之數據存儲

編輯:初級開發

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.*

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved