ANDROID CONTENT PROVIDERS Content Provider A content provider

  • Slides: 19
Download presentation
ANDROID – CONTENT PROVIDERS

ANDROID – CONTENT PROVIDERS

Content Provider A content provider makes a specific set of the application's data available

Content Provider A content provider makes a specific set of the application's data available to other applications. If you don't need to share data amongst multiple applications you can use a database directly via SQLite. Database.

Built-in Content Providers Contacts Media. Store. Audio Media. Store. Images Media. Store. Video Browser

Built-in Content Providers Contacts Media. Store. Audio Media. Store. Images Media. Store. Video Browser Call. Log Settings

Basic Idea – URI format Data mapped to a URI (CONTENT_URI) URI is used

Basic Idea – URI format Data mapped to a URI (CONTENT_URI) URI is used to access data by client <standard_prefix>: //<authority>/<data_path>/<id > examples: content: //media/internal/images = list of all internal images on device content: //media/external/images = list of all external images on device content: //call_log/calls = list of all calls in Call Log content: //browser/bookmarks = list of bookmarks

Basic Idea – example for Contacts Content Provider Data mapped to a URI (CONTENT_URI)

Basic Idea – example for Contacts Content Provider Data mapped to a URI (CONTENT_URI) URI is used to access data by client content: //contacts/people/ All contact names content: //contacts/people/23 Contact with _ID = 23

CONTENT_URI Content URI can be complex Rather than hardcoding exact URI everywhere Content Providers

CONTENT_URI Content URI can be complex Rather than hardcoding exact URI everywhere Content Providers expose the base URI as static field Name. Provider. CONTENT_URI public static final String AUTHORITY = “packagename. Name. Provider"; public static final Uri CONTENT_URI = Uri. parse("content: //" + AUTHORITY + "/names");

Option 1: Accessing data with a Content Provider using Cursor. Loader cursor Loader =

Option 1: Accessing data with a Content Provider using Cursor. Loader cursor Loader = new Cursor. Loader(Context context, Uri uri, String[ ] projection, String selection, String[ ] selection. Args, String sort. Order) OPTION 1: This is for Honeycomb Or Later version of Android context = associated context uri = Content Provider URI projection =which columns to return selection = SQL Where clause with "WHERE" selection. Args =Arguments for selection sort. Order = SQL ORDER BY clause

Option 1: Example Accessing Content Provider data with Cursor. Loader This example: import android.

Option 1: Example Accessing Content Provider data with Cursor. Loader This example: import android. content. Cursor. Loader; we ask for all contacts Loads in background –does NOT block application UI // INSIDE Activity Class **** @Override public void on. Create(Bundle saved. Instances) { //*** other code***** Uri all. Contacts = Uri. parse(“content: //contacts/people”); Cursor. Loader cursor. Loader = new Cursor. Loader( this, all. Contacts, //URI of content provider null, //means return all columns null, // WHERE clause-- won't specify. null, // no where clause, so no arguments null); //no order by specified //get data from Content Provider, populates Cursor c with result set Cursor c = cursor. Loader. load. In. Background(); //LOADS in background, no blocking

Option 1: Example Accessing Content Provider data with Cursor. Loader This example: import android.

Option 1: Example Accessing Content Provider data with Cursor. Loader This example: import android. widget. Simple. Cursor. Adapter; import android. database. Cursor; display results using a Simple. Cursor. Adapter import android. widget. Cursor. Adapter; import android. provider. Contacts. Contract; //built in contacts content provider info // INSIDE Activity Class **** @Override public void on. Create(Bundle saved. Instances) { Cursor. Adapter. FLAG_REGIST ER_CONTENT_OBSERVER Means this adapter registered to be informed when change in content provider //*** other code SEE PREVIOUS SLIDE***** results in c (Cursor instance) //info will display from Content. Provider results in Cursor c String[] columns = new String[] {Contacts. Contract. Contacts. DISPLAY_NAME, Contacts. Contract. Contacts. _ID}; int[] views = new int[] {R. id. contact. Name, R. id. contact. ID}; adapter = new Simple. Cursor. Adapter(this, R. layout. main, c, columns, views Cursor. Adapter. FLAG_REGISTER_CONTENT_OBSERVER);

Option 1: Example ---- what is Simple. Cursor. Adapter An Adapter used to represent

Option 1: Example ---- what is Simple. Cursor. Adapter An Adapter used to represent Cursor (data result set) Used to populate a related View example …. android. app. List. Activity as one possibility List. Activity. Instance. set. List. Adapter(Simple. Cursor. Adapter_Instance) public Simple. Cursor. Adapter ( Context context, int layout, Cursor c, String[] from, int[] to, int flags) context = context where the List. View associated with this layout = resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to" c = database cursor. from =list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet. to = views that should display column in the "from" parameter. These should all be Text. Views. The first N views in this list are given the values of the first N columns in the from parameter. flags = Flags used to determine the behavior of the adapter, as per Cursor. Adapter(Context, Cursor, int).

Example Main. xml ---interface for app’s main Activity (a List. Activity) <Text. View <?

Example Main. xml ---interface for app’s main Activity (a List. Activity) <Text. View <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width="fill_parent" android: layout_height="fill_parent" android: id="@+id/contact. Name" android: text. Style="bold" android: layout_width="wrap_content" android: layout_height="wrap_content" /> android: orientation="vertical" > <Text. View <List. View android: id="@+id/android: list" android: layout_width="fill_parent" android: id="@+id/contact. ID" android: layout_width="fill_parent" android: layout_height="wrap_content" /> android: layout_height="wrap_content" android: layout_weight="1" android: stack. From. Bottom="false" android: transcript. Mode="normal" /> </Linear. Layout>

Example --- main Activity Class public class Provider. Activity extends List. Activity { c

Example --- main Activity Class public class Provider. Activity extends List. Activity { c = cursor. Loader. load. In. Background(); /** Called when the activity is first created. */ @Override String[] columns = new String[] { public void on. Create(Bundle saved. Instance. State) { Contacts. Contract. Contacts. DISPLAY_NAME, super. on. Create(saved. Instance. State); Contacts. Contract. Contacts. _ID}; set. Content. View(R. layout. main); Uri all. Contacts = Contacts. Contract. Contacts. CONTENT_URI; int[] views = new int[] {R. id. contact. Name, R. id. contact. ID}; String[] projection = new String[] {Contacts. Contract. Contacts. _ID, Simple. Cursor. Adapter adapter; Contacts. Contract. Contacts. DISPLAY_NAME, Contacts. Contract. Contacts. HAS_PHONE_NUMBER}; adapter = new Simple. Cursor. Adapter( this, R. layout. main, c, columns, views, Cursor. Adapter. FLAG_REGISTER_CONTENT_OBSERVER); Cursor c; Cursor. Loader cursor. Loader = new Cursor. Loader( this, this. set. List. Adapter(adapter); all. Contacts, projection, Contacts. Contract. Contacts. DISPLAY_NAME + " LIKE ? “, new String[] {"%Lee"}, Contacts. Contract. Contacts. DISPLAY_NAME + " ASC"); Print. Contacts(c); }

Example --- main Activity Class continued private void Print. Contacts(Cursor c) { if (c.

Example --- main Activity Class continued private void Print. Contacts(Cursor c) { if (c. move. To. First()) { do{ String contact. ID = c. get. String(c. get. Column. Index( Contacts. Contract. Contacts. _ID)); String contact. Display. Name = c. get. String(c. get. Column. Index( Contacts. Contract. Contacts. DISPLAY_NAME)); Log. v("Content Providers", contact. ID + ", " + contact. Display. Name); } while (c. move. To. Next()); } } } <<< UTILITY Method to print out the result set returned in the Cursor instance c that contains all the Contacts.

Example ---some explanation Predefined Query String Constants Uri all. Contacts = Contacts. Contract. Contacts.

Example ---some explanation Predefined Query String Constants Uri all. Contacts = Contacts. Contract. Contacts. CONTENT_URI SAME AS Uri all. Contacts = Uri. parse(“content: //contacts/people”);

Example ---some explanation Following is like saying give me all the contacts with the

Example ---some explanation Following is like saying give me all the contacts with the columns ID, DISPLAY_NAME and HAS_PHONE_NUMBER where the DISPLAY_NAME is Like Lee and orderby DISPLAY_NAME in Ascending order (ASC) String[] projection = new String[] {Contacts. Contract. Contacts. _ID, Contacts. Contract. Contacts. DISPLAY_NAME, Contacts. Contract. Contacts. HAS_PHONE_NUMBER}; Cursor c; Cursor. Loader cursor. Loader = new Cursor. Loader( this, all. Contacts, projection, Contacts. Contract. Contacts. DISPLAY_NAME + " LIKE ? “, new String[] {"%Lee"}, Contacts. Contract. Contacts. DISPLAY_NAME + " ASC");

Predefined Query Strings Predefined Query String Constants Uri all. Contacts = Contacts. Contract. Contacts.

Predefined Query Strings Predefined Query String Constants Uri all. Contacts = Contacts. Contract. Contacts. CONTENT_URI SAME AS Uri all. Contacts = Uri. parse(“content: //contacts/people”); Other examples Browser. BOOKMARKS_URI Browser. SEARCHES_URI Call. Log. CONTENT_URI Media. Store. Images. Media. INTERNAL_CONTENT_URI Media. Store. Images. Media. EXPERNAL_CONTENT_URI Settings. CONTENT_URI

Another option to query Content Provider Older method

Another option to query Content Provider Older method

Option 2: Example with managed. Query(*) Android. net. Uri all. Contacts; all. Contacts =

Option 2: Example with managed. Query(*) Android. net. Uri all. Contacts; all. Contacts = Uri. parse(“content: //contacts/people”); Cursor c = managed. Query(all. Contacts, null, null); //gets all contacts OPTION 2: ONLY FOR HISTORIC NEEDS!!!!! OPTION 2: This is for prior to Honeycomb Version of Android

Option 2: Accessing data with a Content Provider using manaed. Query(*) public final Cursor

Option 2: Accessing data with a Content Provider using manaed. Query(*) public final Cursor managed. Query(Uri uri, String[] projection, String selection, String[] selection. Args, String sort. Order) OPTION 2: uri = Content Provider URI projection =which columns to return selection = SQL Where clause with "WHERE" selection. Args =Arguments for selection sort. Order = SQL ORDER BY clause This is for prior to Honeycomb Version of Android