Android Content Providers Prof Sainath Patil VCET Vasai

  • Slides: 56
Download presentation
Android Content Providers Prof. Sainath Patil VCET Vasai Rd. 1

Android Content Providers Prof. Sainath Patil VCET Vasai Rd. 1

Lecture Outcomes At the end of this lecture you will able to; • Understand

Lecture Outcomes At the end of this lecture you will able to; • Understand content provider • Understand how to access content provider Prof. Sainath Patil VCET Vasai Rd. 2

Content provider • A content provider component supplies data from one application to others

Content provider • A content provider component supplies data from one application to others on request. • Such requests are handled by the methods of the Content. Resolver class. • A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network. Prof. Sainath Patil VCET Vasai Rd. 3

Content provider • A content provider component supplies data from one application to others

Content provider • A content provider component supplies data from one application to others on request. ns. o i t a c i l pp a s s o r c ta a a d e ful. r e a s h u s y o r t e • Such requestsreare handled by theemethods e v of the red i m u o q c b s ers s it i d i e v m o i r t Content. Resolver class. e p nt e Som t n o c e er h w s i Th • A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network. Prof. Sainath Patil VCET Vasai Rd. 4

Content provider • A content provider manages access to a central repository of data.

Content provider • A content provider manages access to a central repository of data. • A provider is part of an Android application, which often provides its own UI for working with the data. • However, content providers are primarily intended to be used by other applications, which access the provider using a provider client object. Prof. Sainath Patil VCET Vasai Rd. 5

Content provider • Typically you work with content providers in one of two scenarios;

Content provider • Typically you work with content providers in one of two scenarios; ü you may want to implement code to access an existing content provider in another application, or ü you may want to create a new content provider in your application to share data with other applications. Prof. Sainath Patil VCET Vasai Rd. 6

Content provider • Typically you work with content providers in one of two scenarios;

Content provider • Typically you work with content providers in one of two scenarios; • you may want to implement code to access an existing content provider in another application, or • • A content behaves very much like a database you mayprovider want to create a new content provider in your where you can querydata it, edit content, as well as add application to share withitsother applications. or delete content using insert(), update(), delete(), and query() methods. • In most cases this data is stored in an SQlite database. Prof. Sainath Patil VCET Vasai Rd. 7

Overview • A content provider presents data to external applications as one or more

Overview • A content provider presents data to external applications as one or more tables that are similar to the tables found in a relational database. • A content provider coordinates access to the data storage layer in your application for a number of different APIs and components as illustrated in figure 1 Prof. Sainath Patil VCET Vasai Rd. 8

Overview • A content provider presents data to external applications as one or more

Overview • A content provider presents data to external applications as one or more tables that are similar to the tables found in a relational database. • A content provider coordinates access to the data storage layer in your application for a number of different APIs and components as illustrated in figure. Figure: . Relationship between content provider and other components. Prof. Sainath Patil VCET Vasai Rd. Ref. : https: //developer. android. com/guide/topics/providers/content-provider-basic 9

Sha ring a othe r ap ccess to plica tions your ap plica tion

Sha ring a othe r ap ccess to plica tions your ap plica tion data w ith Prof. Sainath Patil VCET Vasai Rd. 10

ng ndi to a t a a et g d wi d Se Prof.

ng ndi to a t a a et g d wi d Se Prof. Sainath Patil VCET Vasai Rd. 11

Re yo turn fra ur a ing us mew ppli cus ing o ca

Re yo turn fra ur a ing us mew ppli cus ing o ca tom Se rk tion s thr ear ar ch ou ch Re gh su ce the gg nt. S se estio ug ar ge ch ns f sti or on s. P ro vid er Prof. Sainath Patil VCET Vasai Rd. 12

Syn c with hronizin imp your se g applic le rv a of A

Syn c with hronizin imp your se g applic le rv a of A mentat er usin tion da bstr ta i g an act. T on hrea ded Syn c. Ad apte r Prof. Sainath Patil VCET Vasai Rd. 13

g r. U you in a t da der g din or. Loa a

g r. U you in a t da der g din or. Loa a Lo urs a. C Prof. Sainath Patil VCET Vasai Rd. sin u I 14

Accessing a provider • To access data in a content provider, use the Content.

Accessing a provider • To access data in a content provider, use the Content. Resolver object in the application's Context to communicate with the provider as a client. Prof. Sainath Patil VCET Vasai Rd. 15

Accessing a provider • To access data in a content provider, use the Content.

Accessing a provider • To access data in a content provider, use the Content. Resolver object in the application's Context to communicate with the provider as a client. • The Content. Resolver object communicates with the provider object, an instance of a class that implements Content. Provider. Prof. Sainath Patil VCET Vasai Rd. 16

Accessing a provider • To access data in a content provider, use the Content.

Accessing a provider • To access data in a content provider, use the Content. Resolver object in the application's Context to communicate with the provider as a client. • The Content. Resolver object communicates with the provider object, an instance of a class that implements Content. Provider. • The provider object receives data requests from clients, performs the requested action, and returns the results. Prof. Sainath Patil VCET Vasai Rd. 17

Accessing a provider • To access data in a content provider, use the Content.

Accessing a provider • To access data in a content provider, use the Content. Resolver object in the application's Context to communicate with the provider as a client. • The Content. Resolver object communicates with the provider object, an instance of a class that implements Content. Provider. • The provider object receives data requests from clients, performs the requested action, and returns the results. • This object has methods that call identically-named methods in the provider object, an instance of one of the concrete subclasses of Content. Provider. Prof. Sainath Patil VCET Vasai Rd. 18

Accessing a provider • To access data in a content provider, use the Content.

Accessing a provider • To access data in a content provider, use the Content. Resolver object in the application's Context to communicate with the provider as a client. • The Content. Resolver object communicates with the provider object, an instance of a class that implements Content. Provider. • The provider object receives data requests from clients, performs the • requested action, and returns the results. The Content. Resolver methods provide the basic • This "CRUD" object has methods call identically-named methods in the (create, that retrieve, update, and delete) provider object, an of one of the concrete subclasses functions of instance persistent storage. of Content. Provider. Prof. Sainath Patil VCET Vasai Rd. 19

Accessing a provider • A common pattern for accessing a Content. Provider from UI

Accessing a provider • A common pattern for accessing a Content. Provider from UI uses a Cursor. Loader to run an asynchronous query in the background. • The Activity in UI call a Cursor. Loader to the query, which in turn gets the Content. Provider using the Content. Resolver. • This allows the UI to continue to be available to the user while the query is running. • This pattern involves the interaction of a number of different objects, as well as the underlying storage mechanism, as illustrated in figure. Prof. Sainath Patil VCET Vasai Rd. 20

Accessing a provider Interaction between • A common pattern for accessing. Figure: a Content.

Accessing a provider Interaction between • A common pattern for accessing. Figure: a Content. Provider from UI Content. Provider, other classes, and storage. uses a Cursor. Loader to run an asynchronous query in the background. • The Activity in UI call a Cursor. Loader to the query, which in turn gets the Content. Provider using the Content. Resolver. • This allows the UI to continue to be available to the user while the query is running. • This pattern involves the interaction of a number of different objects, as well as the underlying storage mechanism, as illustrated in figure. Ref. : https: //developer. android. com/guide/topics/providers/content-provider-basic Prof. Sainath Patil VCET Vasai Rd. 21

Accessing a provider • One of the built-in providers in the Android platform is

Accessing a provider • One of the built-in providers in the Android platform is the user dictionary, which stores the spellings of non-standard words that the user wants to keep. word app id frequency locale _ID mapreduce user 1 100 en_US 1 precompiler user 14 200 fr_FR 2 applet user 2 225 fr_CA 3 const user 1 255 pt_BR 4 int user 5 100 en_UK 5 Table: Sample user dictionary table. Ref. : https: //developer. android. com/guide/topics/providers/content-provider-basic Prof. Sainath Patil VCET Vasai Rd. 22

Accessing a provider • One of the built-in providers in the Android platform is

Accessing a provider • One of the built-in providers in the Android platform is the user dictionary, which stores the spellings of non-standard words that the user wants to keep. word app id frequency locale _ID mapreduce user 1 100 en_US 1 user 14 200 2 • Toprecompiler refer to a row's locale, you refer to its fr_FR locale column. 225 3 key" • Forapplet this provider, user 2 the _ID column serves fr_CA as a "primary column automatically maintains. const that the provider user 1 255 pt_BR 4 int user 5 100 en_UK 5 Table: Sample user dictionary table. Prof. Sainath Patil VCET Vasai Rd. 23

Accessing a provider • To get a list of the words and their locales

Accessing a provider • To get a list of the words and their locales from the User Dictionary Provider, call Content. Resolver. query(). • The query() method calls the Content. Provider. query() method defined by the User Dictionary Provider. • The following lines of code show Content. Resolver. query() call: Prof. Sainath Patil VCET Vasai Rd. 24

Accessing a provider • To get a list of the words and their locales

Accessing a provider • To get a list of the words and their locales from the User Dictionary Provider, call Content. Resolver. query(). the usermethod dictionary and the returns results • // Queries The query() calls Content. Provider. query() method cursor = get. Content. Resolver(). query( defined by the User Dictionary Provider. User. Dictionary. Words. CONTENT_URI, // The content URI of the words table • The following lines of// The codecolumns show to Content. Resolver. query() projection, return for each row call: selection. Clause, // Selection criteria selection. Args, sort. Order ); // Selection criteria // The sort order for the returned rows Ref. : https: //developer. android. com/guide/topics/providers/content-provider-basic Prof. Sainath Patil VCET Vasai Rd. 25

How the arguments to query(Uri, projection, selection. Args, sort. Order) match an SQL SELECT

How the arguments to query(Uri, projection, selection. Args, sort. Order) match an SQL SELECT statement: Prof. Sainath Patil VCET Vasai Rd. 26

How the arguments to query(Uri, projection, selection. Args, sort. Order) match an SQL SELECT

How the arguments to query(Uri, projection, selection. Args, sort. Order) match an SQL SELECT statement: query() argument SELECT keyword/parameter Uri FROM table_name Uri maps to the table in the provider named table_name. projection col, . . . projection is an array of columns that should be included for each row retrieved. selection WHERE col = value selection specifies the criteria for selecting rows. selection. Args (No exact equivalent. Selection arguments replace ? placeholders in the selection clause. ) sort. Order ORDER BY col, . . . Notes sort. Order specifies the order in which rows appear in the returned Cursor. Table: Query() compared to SQL query. Ref. : https: //developer. android. com/guide/topics/providers/content-provider-basic Prof. Sainath Patil VCET Vasai Rd. 27

Content URIs • A content URI is a URI that identifies data in a

Content URIs • A content URI is a URI that identifies data in a provider. • Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path). • When you call a client method to access a table in a provider, the content URI for the table is one of the arguments. Prof. Sainath Patil VCET Vasai Rd. 28

Content URIs The query string in the form of a URI which has following

Content URIs The query string in the form of a URI which has following format − <prefix>: //<authority>/<data_type>/<id> Prof. Sainath Patil VCET Vasai Rd. 29

Content URIs The query string in the form of a URI which has following

Content URIs The query string in the form of a URI which has following format − <prefix>: //<authority>/<data_type>/<id> prefix This is always set to content: // Prof. Sainath Patil VCET Vasai Rd. 30

Content URIs The query string in the form of a URI which has following

Content URIs The query string in the form of a URI which has following format − <prefix>: //<authority>/<data_type>/<id> authority • This specifies the name of the content provider, for example contacts, browser etc. • For third-party content providers, this could be the fully qualified name, such as com. example. sprovider Prof. Sainath Patil VCET Vasai Rd. 31

Content URIs The query string in the form of a URI which has following

Content URIs The query string in the form of a URI which has following format − <prefix>: //<authority>/<data_type>/<id> data_type (path) • This indicates the type of data that this particular provides. • For example, if you are getting all the contacts from the Contacts content provider, then the data path would be people and URI would look like this content: //contacts/people Prof. Sainath Patil VCET Vasai Rd. 32

Content URIs The query string in the form of a URI which has following

Content URIs The query string in the form of a URI which has following format − <prefix>: //<authority>/<data_type>/<id> id • This specifies the specific record requested. • For example, if you are looking for contact number 5 in the Contacts content provider then URI would look like this content: //contacts/people/5. Prof. Sainath Patil VCET Vasai Rd. 33

Content URIs • In the previous lines of code, the constant CONTENT_URI contains the

Content URIs • In the previous lines of code, the constant CONTENT_URI contains the content URI of the user dictionary's "words" table. // Queries the user dictionary and returns results cursor = get. Content. Resolver(). query( User. Dictionary. Words. CONTENT_URI, // The content URI of the words table projection, selection. Clause, selection. Args, sort. Order ); // The columns to return for each row // Selection criteria // The sort order for the returned rows Prof. Sainath Patil VCET Vasai Rd. 34

Content URIs • The Content. Resolver object parses out the URI's authority, and uses

Content URIs • The Content. Resolver object parses out the URI's authority, and uses it to "resolve" the provider by comparing the authority to a system table of known providers. • The Content. Resolver can then dispatch the query arguments to the correct provider. • The Content. Provider uses the path part of the content URI to choose the table to access. A provider usually has a path for each table it exposes. Prof. Sainath Patil VCET Vasai Rd. 35

Content URIs In the previous lines of code, the full URI for the "words"

Content URIs In the previous lines of code, the full URI for the "words" table is: // Queries the user dictionary and returns results cursor = get. Content. Resolver(). query( User. Dictionary. Words. CONTENT_URI, // The content URI of the words table projection, // The columns to return for each row content: //user_dictionary/words selection. Clause, selection. Args, // Selection criteria where the user_dictionary string is the provider's authority, and sort. Order ); // The sort order for the returned rows the words string is the table's path. Prof. Sainath Patil VCET Vasai Rd. 36

Create Content Provider This involves number of simple steps to create your own content

Create Content Provider This involves number of simple steps to create your own content provider. Prof. Sainath Patil VCET Vasai Rd. 37

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. Prof. Sainath Patil VCET Vasai Rd. 38

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. public class Students. Provider extends Content. Provider { } Prof. Sainath Patil VCET Vasai Rd. 39

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. Prof. Sainath Patil VCET Vasai Rd. 40

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. static final String PROVIDER_NAME = "com. example. My. Application. Students. Provider"; static final String URL = "content: //" + PROVIDER_NAME + "/students"; static final Uri CONTENT_URI = Uri. parse(URL); Prof. Sainath Patil VCET Vasai Rd. 41

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. 3. Create your own database to keep the content. Prof. Sainath Patil VCET Vasai Rd. 42

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. 3. Create your own database to keep the content. Usually, Android uses SQLite database. Prof. Sainath Patil VCET Vasai Rd. 43

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. 3. Create your own database to keep the content. private SQLite. Database db; static final String DATABASE_NAME = "College"; static final String STUDENTS_TABLE_NAME = "students"; static final int DATABASE_VERSION = 1; static final String CREATE_DB_TABLE = " CREATE TABLE “ + STUDENTS_TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + " name TEXT NOT NULL, " + " grade TEXT NOT NULL); "; Prof. Sainath Patil VCET Vasai Rd. 44

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. 3. Create your own database to keep the content. Usually, Android uses SQLite database. And uses framework to override on. Create() method. Prof. Sainath Patil VCET Vasai Rd. 45

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. 3. Create your own database to keep the content. Usually, Android uses SQLite database. And uses framework to override on. Create() method. @Override public boolean on. Create() { Context context = get. Context(); Database. Helper db. Helper = new Database. Helper(context); db = db. Helper. get. Writable. Database(); return (db == null)? false: true; } Prof. Sainath Patil VCET Vasai Rd. 46

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. 3. Create your own database to keep the content. Usually, Android uses SQLite database. And uses framework to override on. Create() method. This on. Create() will use SQLite. Open. Helper() method to create or open the provider’s database. Prof. Sainath Patil VCET Vasai Rd. 47

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. 3. Create your own database to keep the content. Usually, Android uses SQLite database. private static class Database. Helper extends And uses framework to override on. Create() method. SQLite. Open. Helper { this on. Create() will use SQLite. Open. Helper() Database. Helper(Context context){method to create or open the provider’s database. DATABASE_NAME, null, super(context, DATABASE_VERSION); } @Override public void on. Create(SQLite. Database db) { db. exec. SQL(CREATE_DB_TABLE); } } Prof. Sainath Patil VCET Vasai Rd. 48

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. 3. Create your own database to keep the content. Usually, Android uses SQLite database. private static class Database. Helper extends And uses framework to override on. Create() method. SQLite. Open. Helper { this on. Create() will useis. SQLite. Open. Helper() method to create Database. Helper(Context When your application launched, context){ the on. Create() handler of or open theitsprovider’s database. is called super(context, DATABASE_NAME, null, each of Content Providers on the main application thread. @Override DATABASE_VERSION); } public void on. Create(SQLite. Database db) { db. exec. SQL(CREATE_DB_TABLE); } } Prof. Sainath Patil VCET Vasai Rd. 49

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. 3. Create your own database to keep the content. Implement Content Provider queries to perform different database specific operations. 4. Finally register Content Provider in Android. Manifest. xml file using <provider> tag. Prof. Sainath Patil VCET Vasai Rd. 50

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider

Create Content Provider 1. Create a Content Provider class that extends the Content. Provider base class. 2. Define content provider URI address which will be used to access the content. <application 3. Create android: allow. Backup="true" your own database to keep the content. Implement Content. . . > Provider queries to perform different database specific operations. android: name=". Main. Activity"> 4. Finally<activity register Content Provider in Android. Manifest. xml file. . . using <provider> tag. </activity> <provider android: name="Students. Provider“ android: authorities= "com. example. My. Application. Students. Provider"/> </application> Prof. Sainath Patil VCET Vasai Rd. 51

List of methods The list of methods which you need to override in Content

List of methods The list of methods which you need to override in Content Provider class to have your Content Provider working Prof. Sainath Patil VCET Vasai Rd. 52

List of methods on. Create() This method is called when the provider is started.

List of methods on. Create() This method is called when the provider is started. Prof. Sainath Patil VCET Vasai Rd. 53

List of methods on. Create() This method is called when the provider is started.

List of methods on. Create() This method is called when the provider is started. query() This method receives a request from a client. The result is returned as a Cursor object. insert()This method inserts a new record into the content provider. Prof. Sainath Patil VCET Vasai Rd. 54

List of methods delete() This method deletes an existing record from the content provider.

List of methods delete() This method deletes an existing record from the content provider. update() This method updates an existing record from the content provider. get. Type() This method returns the MIME type of the data at the given URI. Prof. Sainath Patil VCET Vasai Rd. 55

Thank You Prof. Sainath Patil VCET Vasai Rd. 56

Thank You Prof. Sainath Patil VCET Vasai Rd. 56