Download presentation
Presentation is loading. Please wait.
Published byKelly Gordon Modified over 8 years ago
1
By: Eliav Menachi
2
On Android, all application data (including files) are private to that application Android provides a standard way for an application to expose its private data to other applications - content providers Content provider exposes read / write access to the application's data Content providers implement a standard syntax for requesting and modifying data Content providers implement a standard mechanism for reading the returned data
3
Preferences Files Database Network Content Providers
4
Preferences is a lightweight mechanism to store and retrieve key - value pairs of primitive data types Typically used to store application preferences Preferences cannot be shared across applications (except by using a content provider)
5
Files can be stored directly on the mobile device or on a removable storage medium By default, other applications cannot access these files To read data from a file: Context. openFileInput which returns a standard Java FileInputStream object Context. openFileInput FileInputStream To write to a file: Context. openFileOutput which returns a FileOutputStream object Context. openFileOutput FileOutputStream
6
Android supports creating and using SQLite databases Each database is private to the application that creates it The SQLiteDatabase object represents a database and has methods for interacting with it SQLiteDatabase To create the database: SQLiteDatabase. create Android ships with the sqlite3 database tool: enables to browse table contents run SQL commands perform other useful functions on SQLite databases All databases, SQLite and others, are stored on the device in / data / data / package_name / databases
7
Store and retrieve data Make data accessible to all applications there's no common storage area that all Android packages can access Android ships with a number of content providers for common data types: Audio Video Images Personal contact information and more (listed in android. provider package)android. provider
8
There are two options to make data public: Create a content provider Add the data to an existing content provider if there's one that controls the same type of data If you have permission to write to it
9
All content providers implement a common interface for querying the provider and returning results, adding, altering, and deleting data. It's an interface that clients use indirectly most generally through ContentResolver objectsContentResolver You get a ContentResolver by calling getContentResolver
10
Content providers expose their data as a simple table on a database model Each row is a record and each column is data of a particular type and meaning _ID field uniquely identifies the record within the table
11
Each content provider exposes a public URI URI uniquely identifies a data set For multiple tables exposes a separate URI for each table Android defines CONTENT_URI constants for all the providers that come with the platform android. provider. Contacts. Phones. CONTENT_URI android.provider.Contacts.Photos.CONTENT_URI
12
A: "content://” - identifies the data as being controlled by a content provider B: “com…/” - An authority - unique identifier used to locate the provider in the provider registry C: “path section of the URI that is specific to each provider D: ID of the row to fetch
13
Three pieces of information required to query a content provider : The URI that identifies the provider The names of the data fields to receive The data types for those fields
14
There are two ways to query content provider: ContentResolver. query Activity. managedQuery Both take the same set of arguments Both return a Cursor object managedQuery causes the activity to manage the life cycle of the Cursor
15
public final Cursor managedQuery (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)CursorUriString[]StringString[]String Uri: The URI of the content provider to query Projection: List of columns to return Selection: SQL WHERE clause selectionArgs: The arguments to selection sortOrder: SQL ORDER BY clause
16
A query returns a set of zero or more database records The retrieved data is exposed by a Cursor objectCursor Cursor can be used to iterate backward or forward through the result set Cursor can be used only to read the data
17
Data kept by a content provider can be modified by : Adding new records Adding new values to existing records Batch updating existing records Deleting records
18
Set up a map of key-value pairs in a ContentValues objectContentValues Each key matches the name of a column in the content provider The value is the desired value for the new record in that column Call ContentResolver.insert and pass it the URI of the provider and the ContentValues map This method returns the full URI of the new record i.e. the provider's URI with the appended ID for the new record
19
Once a record exists, you can add new information to it or modify existing information The best way to add to a record in the database is to append the name of the table where the new data goes to the URI for the record then use the amended URI to add the new data values
20
To update a single record: ContentResolver. update with the URI of a specific row To update multiple rows: ContentResolver. update with the URI of the type of record to delete and an SQL WHERE clause defining which rows to update
21
public final int update (Uri uri, ContentValues values, String where, String[] selectionArgs) Values: The new field values A null value will remove an existing field value Where: A filter to apply to rows before deleting, formatted as an SQL WHERE clause (excluding the WHERE itself) Returns The number of rows updated.
22
To delete a single record: ContentResolver. delete with the URI of a specific row To delete multiple rows: ContentResolver. delete with the URI of the type of record to delete and an SQL WHERE clause defining which rows to delete
23
Int android.content.ContentResolver.delete (Uri url, String where, String[] selectionArgs) url: the url of the row/table to delete where:A filter to apply to rows before deleting, formatted as an SQL WHERE clause (excluding the WHERE itself) selectionArgs: arguments for the where clause Returns the number of rows deleted
24
To create a content provider: Set up a system for storing the data. Most content providers store their data using Android's file storage methods or SQLite databases Extend the ContentProvider class to provide access to the data. ContentProvider Declare the content provider in the manifest file for your application
25
Subclass ContentProviderContentProvider Implementing six abstract methods: query () insert() update() delete() getType() onCreate()
26
The query method must return a Cursor object that can iterate over the requested data Cursor Cursor itself is an interface, but Android provides some ready - made Cursor objects that you can use For example, SQLiteCursor can iterate over data stored in an SQLite database SQLiteCursor ContentProvider methods can be called from various ContentResolver objects in different processes and threads, they must be implemented in a thread-safe manner! Define CONTENT_URI constants for each of the subtables content :// com. example. codelab. transporationprovider / train content :// com. example. codelab. transporationprovider / air / domestic content :// com. example. codelab. transporationprovider / air / inter
27
List running devices: adb devices Open a shell to a device: adb shell location of all shell commands: /system/bin list with permissions: Ls –l list of databases packages: ls /data/data List contact databases: ls /data/data/com.android.providers.contacts/databases pull contacts. db down to your local machine: adb pull /data/data/com.android.providers.contacts/databases/con tacts.db c:/somelocaldir/contacts.db
28
http://www.sqlite.org/ http://www.sqlite.org/ SQLite databases on the device are created as needed
29
Open: sqlite3 /data/data/com.android.providers.contacts/databases/con tacts.db Exit:.exit List the tables:.tables prints out a create statement for a table:.schema Set the column headers to show in the tool sqlite>.headers on select all rows from a table: select * from table1; count the number of rows in a table: select count(*) from table1;
30
select a specific set of columns: select col1, col2 from table1; Select distinct values in a column: select distinct col1 from table1; counting the distinct values: select count(col1) from (select distinct col1 from table1); group by: select count(*), col1 from table1 group by col1; regular inner join: select * from table1 t1, table2 t2 where t1.col1 = t2.col1; left outer join, Give me everything in t1 even though there are no rows in t2: select * from table t1 left outer join table2 t2 on t1.col1 = t2.col1 where....
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.