Android Programing Bài 8: Asset (part 1)

5 64 0
Android Programing Bài 8: Asset (part 1)

Đang tải... (xem toàn văn)

Thông tin tài liệu

BÀI 8: Asset - Shared preference - Memory Device Khái niệm lưu trữ SharedPreference cho phép thực lưu trữ truy xuất liệu theo cặp khóa key-value cho phép kiểu liệu Có thể lưu trữ kiểu liệu sau: Boolean Float Int Long String Dữ liệu bảo toàn ứng dụng bị đóng hồn tồn 1.2 Khai báo sử dụng Khai báo truy xuất: ◦ Có thể sử dụng hai phương thức sau  getSharePreferences: sử dụng truy vấn đến tập tin Preferences lưu trữ cách truyền vào tập tin  getPreferences: truy xuất đến tập tin Preference mặc định tương ứng với Activity gọi thực thi ◦ Mặc định tập tin presference lưu trữ theo đường dẫn: Data/data/ 1.2 Khai báo sử dụng  Khai báo truy xuất: ◦ Các định dạng mở tập tin truy xuất Preference:  MODE_PRIVATE  MODE_APPEND  MODE_WORLD_READABLE(Deprecated in API 17)  MODE_WORLD_WRITEABLE(Deprecated in API 17)  MODE_MULTI_PROCESS  Lưu trữ liệu: ◦ Tạo đối tượng Editor từ phương thức edit() preferece ◦ Lưu trữ liệu vào preference thông qua phương thức put  putBoolean(Key,Value)  putFloat(Key,value)  putInt(Key,value)  putLong(Key,value)  putString(Key,Value) Ví dụ: boolean wifi_status=checkBoxWifi.isChecked() SharePreference share_pref=getPreferences(MODE_PRIVATE); Editor editor=shared_pref.edit(); Editor.putBoolean(“Wifi_Status”, wifi_status); Editor.apply();  Khai báo truy xuất ◦ Các giá trị lưu trữ truy xuất thông qua phương thức get:  getBoolean(Key, DefValue)  getFloat(Key,DefValue)  getInt(Key,DefValue)  getLong(Key,DefValue)  getString(Key,DefValue) ◦ Ví dụ: boolean wifi_status= getPreferences(MODE_PRIVATE).getBoolean(“Wifi_Status”); checkBoxWifi.setChecked(wifi_status); 2.1 Bộ nhớ  Bộ nhớ cấp phát ứng dụng cài đặt thiết bị, thư mục riêng biệt:  Chỉ có ứng dụng truy xuất nhớ ứng dụng ◦ Đường dẫn tập tin lưu trữ:  Data/data//files ◦ Phương thức truy xuất:  getFilesDir_File  Truy xuất ghi tập tin: ◦ Gọi phương thức openFileOutput() nhận liệu stream cho đối tượng FileOutStream ◦ Đọc liệu với phương thức write() ◦ Đóng stream phương thức close() ◦ Ví dụ: String hello=“Hello Android!”; FileOutStream fos=openFileOutput(“Hello_file.txt”, MODE_APPEND); fos.write(hello.getBytes()); fos.close();  Truy xuất đọc tập tin: ◦ Gọi phương thức openFileInput() nhận liệu stream cho đối tượng FileInputStream ◦ Đọc liệu với phương thức read() ◦ Đọc stream phương thức close() ◦ Ví dụ: FileInputStream fis=openFileInput(“Hello_file.txt”); int b=0; String s=“”; While((b=fis.read())!=0) s +=(char)b; Fis.close(); Log.d(“HTSI”,s); 2.2 Bộ nhớ  Mỗi thiết bị Android cung cấp nhớ ngồi cho phép người dùng luuw trữ truy xuất trực tiếp thiết bị thơng qua máy tính kết nối USB Storage  Bộ lưu trữ bao gồm hai dạng: ◦ Bộ nhớ thiết bị(non-removeable) ◦ Bộ nhớ ngoài(sd-card, usb…)  Cần xin cấp quyền truy cập nhớ  Ứng dụng cấp phát nhớ để lưu trữ liệu, mặc định truy xuất tập tin Media ◦ Địa lưu trữ: /mnt/sdcard/Android//files ◦ Phương thức truy xuất:  getExternalFilesDir(String)-File  getExternalFilesDir(String)-File[](API 19 – KITKAT)  ContextCompat.getExternalFilesDirs(String)-File[](Support Library)  Cấu hình cài đặt ứng dụng nhớ ngồi thơng qua cặp thể AndroidManifest ◦ Instalocation  internalOnly  Auto  preferExternal  Kiểm tra tình trạng nhớ: ◦ Cần kiểm tra tình trạng nhớ ngồi trước thực thao tác ◦ Truy xuất trạng thái nhớ ngồi thơng qua phương thức getExternalStorageState đối tượng Environment ◦ Các thông số trả vè bao gồm: ◦ MEDIA_UNKNOWN ◦ MEDIA_REMOVED ◦ MEDIDA_UNMOUNTED ◦ MEDIA_CHECKING ◦ MEDIA_NOFS ◦ MEDIA_MOUNTED ◦ MEDIA_MOUNTED_READ_ONLY ◦ MEDIA_SHARED ◦ MEDIA_BAD_REMOVAL ◦ MEDIA_UNMOUNTEDABLE  Kiểm tra tình nhớ ◦ Ví dụ kiểm tra đọc ghi nhớ ngồi Public boolean isExternalStorageWritable(){ String state=Enviroment.getExternalStorageState(); If(Enviroment.MEDIA_MOUNTED.equals(state)) return true; return false; }  Một số phương thức hỗ trợ duyệt tập tin nhớ ◦ isFile()-boolean ◦ isDirectory()-boolean ◦ getAbsolutePath()-String ◦ getPath()-String ◦ list()-String ◦ mkdir()-boolean ◦ mkdirs()-boolean ◦ delete()-boolean  Một số thư mục dùng chung: ◦ Thư mục  DIRECTORY_ALARMS  DIRECTORY_DCIM  DIRECTORY_DOCUMENTS (API 19)  DIRECTORY_DOWNLOADS       DIRECTORY_MOVIES DIRECTORY_MUSIC DIRECTORY_NOTIFYCATIONS DIRECTORY_PICTURE DIRECTORY_POSCASTS DIRECTORY_RINGTONES Asset  Thư mục sử dụng chứa tập tin không biên dịch Project ◦ Truy xuất đối tượng quản lý:  getAssets()-AssetManager ◦ Các phương thức xử lý  List(string path)-String[]  Open(String fileName)-InputStream  Open(String fileName, int mode  Truy xuất phông chữ thông qua thư mục Asset ◦ Sử dụng phương thức createFromAsset ◦ Ví dụ: Typeface arial=Typeface.create.createFromAsset(getAssets(), “font/arial.ttf”); TextView.set Typeface(arial)

Ngày đăng: 22/07/2019, 14:56

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan