Sunteți pe pagina 1din 22

Data Storage, Retrieval, and Sharing

Huang Xuguang xuguangmit@gmail.com 2009.11.17

Outline
SharePerferences Filesystem SQLite ContentProviders

Storing and retrieving data


Five manners
SharePreferences
The simplest one User-specified configuration details Only shared among the activity in an app.

Using filesystem
Files pre-packaged with your application Files created on-device by the app

SQLite
Pop embedded database, public domain Using SQL interface, easy to operate Not JDBC on Android

Content provider
Encapsulation data using Uri instances as handles Perform basic CRUD operations Using existing content providers or create your own

Networking

SharePreferences
Accessing a SharedPreferences object through the Context Many Android classes are reference to or extend from Context, such as Activity, Service Using getSharedPreferences(string name, int accessmode) method to get a preferences handles Access modes
Context.MODE_PRIVATE (value 0) Context.MODE_WORLD_READABLE (value 1) Context.MODE_WORLD_WRITEABLE (value 2)

SharedPreferences XML file path:


data/data/PACKAGE_NAME/shared_prefs

Example: Exlogin demo

Filesystem
Several ways to access the filesystem
Files from within applications Raw files that are included as resources Custom XML files

Files within application


Create a stream to a file: openFileOutput(String name, int mode) Read in the file: openFileInput(String name,int mode) Using standard Java once you get the stream Data path: data/data/[PACKAGE_NAME]/files/files.name Remember to flush it and close it

Raw files
Get resource: Context.getResources() Link to your item: openRawResources(int id) Any type of raw file such as text file, images, document and other file Using raw resource is that they are not precompiled by the platform

XML file
A bit differently than other resources Do not use a stream to access Compiled into an efficient binary form by the platform

Database in Android
SQLite:
Open source Standards-compliant Lightweight Single-tier Choice by MP3 player, iphone, ipod touch etc..

Using SQLite library to provide full relational DB capabilities. Reduces:


External dependencies Minimizes latency Simplifies transaction locking Synchronization

All databases are private: only the app. that created them can access them Using Content Providers to share a database across applications

Work with SQLite(1/4)


Two classes
ContentValues
It represents a single row It used to insert new rows into tables(Content Providers)

Cursor
Queries are returned as Cursor object Pointer, rather than a copy of the result Several functions to navigate query result
moveToFirst moveToNext

Manage Cursor resources


startManagingCursor() stopManagingCursor()

Work with SQLite(2/4)


SQLiteOpenHelper class
Used to simplify open, create, upgrade methods

Work with SQLite(3/4)


Open/Close Database

Insert/Remove record

Work with SQLite(4/4)


Query

Update

Content Provider
Content Providers let you decouple your app. layer from the data layer
App Data App
ContentProvider

Simple URI model: feature full permission control add, remove, update data from other apps.

Data
ContentResolver

Many native Android apps have been available as Content Providers Such as Contact Manager, media player, browser, callLog, Settings
Can create your OWN content provider

Other App

Other App

Using Content Providers


ContentProvider
A set of standard interfaces Query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) Insert(Uri uri, ContentValues values) Update(Uri uri, ContentValues values, String where, String[] selectionArgs) Delete(Uri uri, String where, String[] selectionArgs) Divulging the data owned by itself Support data operations

ContentResolver
External apps access the data supplied by ContentProvider getContentResolver() A set of standard interfaces

URI
All records E.g. content://contacts/people/ information of all the people Specify record by ID E.g. content://contacts/people/1 information of the people whose id is 1 First part: content:// FIXED Second part: contacts Authorization, Content Providers name Third part: people Type of data Fourth part: 1 Optional. NULL: all data; Num: specify record

Example
Access the contact data

Create your Content Provider(1/3)


Extend ContentProvider class
A A: Using parse() to parse URI B: UriMatcher class B
Case 1: Uri=com.paad.provider.myApp/item Return: ALLROWS Case 2: Uri=com.paad.provider.myApp/item/1 Return: SINGLE_ROW

Add <provider> label in AndroidManifest.xml file <provider android:name=ContentProvider android: authorities=com.paad.provider.myapp />

Create your Content Provider(1/3)


Query
1. Achieve the interface 2. Use UriMatcher class 3. Query method: return cursor 4. Insert method: return URI

Insert

5. withAppendedId(para a, para b): Link two parameters as a URI

Create your Content Provider(1/3)


Delete

Update

Case Study
Notepad
Notepad.java
Notes_list.xml Notes_row.xml onCreate(Bundle savedInstanceState) fillData() onCreateOptionsMenu(Menu menu) onMenuItemSelected(int featureId, MenuItem item) onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) onContextItemSelected(MenuItem item) createNote() inListItemClick(ListView l, View v, int position, long id) onActivityResult(int requestCode, int resultCode, Intent intent) onCreate(Bundle savedInstanceState) setOnClickListener(new View.OnClickListener()

NoteEdit.java Note_row.xml

Practices
Using SQLite to manage the contactors in your Address Book app. Create a Content Provider for your Contactor database.

S-ar putea să vă placă și