Documente Academic
Documente Profesional
Documente Cultură
Outline
SharePerferences Filesystem SQLite ContentProviders
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)
Filesystem
Several ways to access the filesystem
Files from within applications Raw files that are included as resources Custom XML files
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..
All databases are private: only the app. that created them can access them Using Content Providers to share a database across applications
Cursor
Queries are returned as Cursor object Pointer, rather than a copy of the result Several functions to navigate query result
moveToFirst moveToNext
Insert/Remove record
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
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
Add <provider> label in AndroidManifest.xml file <provider android:name=ContentProvider android: authorities=com.paad.provider.myapp />
Insert
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.