<blockquote >
<p >A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver 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.</p>
</blockquote>
<p><img alt="content provider" src="http://www.tutorialspoint.com/android/images/content.jpg" /></p>
<h4 align="center" >CONTENTPROVIDER</h4>
<blockquote ><b >sometimes it is required to share data across applications. This is where content providers become very useful.</b></blockquote>
<p >Content providers let you centralize content in one place and have many different applications access it as needed. A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content using insert(), update(), delete(), and query() methods. In most cases this data is stored in an<span class="Apple-converted-space"></span><b >SQlite</b><span class="Apple-converted-space"></span>database.</p>
<p >A content provider is implemented as a subclass of<span class="Apple-converted-space"></span><b >ContentProvider</b><span class="Apple-converted-space"></span>class and must implement a standard set of APIs that enable other applications to perform transactions.</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >My</span><span class="pln" > </span><span class="typ" >Application</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >ContentProvider</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="pun" >}</span></pre>
<h2 >Content URIs</h2>
<p >To query a content provider, you specify the query string in the form of a URI which has following format:</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="tag" ><prefix></span><span class="pln" >://</span><span class="tag" ><authority></span><span class="pln" >/</span><span class="tag" ><data_type></span><span class="pln" >/</span><span class="tag" ><id></span></pre>
<p >Here is the detail of various parts of the URI −</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th >Part</th>
<th >Description</th>
</tr>
<tr >
<td >prefix</td>
<td >This is always set to content://</td>
</tr>
<tr >
<td >authority</td>
<td >This specifies the name of the content provider, for example<span class="Apple-converted-space"></span><i >contacts</i>,<i >browser</i><span class="Apple-converted-space"></span>etc. For third-party content providers, this could be the fully qualified name, such as<span class="Apple-converted-space"></span><i >com.tutorialspoint.statusprovider</i></td>
</tr>
<tr >
<td >data_type</td>
<td >This indicates the type of data that this particular provider provides. For example, if you are getting all the contacts from the<span class="Apple-converted-space"></span><i >Contacts</i>content provider, then the data path would be<span class="Apple-converted-space"></span><i >people</i><span class="Apple-converted-space"></span>and URI would look like this<span class="Apple-converted-space"></span><i >content://contacts/people</i></td>
</tr>
<tr >
<td >id</td>
<td >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<span class="Apple-converted-space"></span><i >content://contacts/people/5</i>.</td>
</tr>
</tbody>
</table>
<h2 >Create Content Provider</h2>
<p >This involves number of simple steps to create your own content provider.</p>
<ul class="list" >
<li >
<p >First of all you need to create a Content Provider class that extends the<i >ContentProviderbaseclass.</i></p>
</li>
<li >
<p >Second, you need to define your content provider URI address which will be used to access the content.</p>
</li>
<li >
<p >Next you will need to create your own database to keep the content. Usually, Android uses SQLite database and framework needs to override<i >onCreate()</i><span class="Apple-converted-space"></span>method which will use SQLite Open Helper method to create or open the provider's database. When your application is launched, the<i >onCreate()</i><span class="Apple-converted-space"></span>handler of each of its Content Providers is called on the main application thread.</p>
</li>
<li >
<p >Next you will have to implement Content Provider queries to perform different database specific operations.</p>
</li>
<li >
<p >Finally register your Content Provider in your activity file using <provider> tag.</p>
</li>
</ul>
<p >Here is the list of methods which you need to override in Content Provider class to have your Content Provider working:</p>
<p><img alt="content provider" src="http://www.tutorialspoint.com/android/images/content1.jpg" /></p>
<h4 align="center" >CONTENTPROVIDER</h4>
<ul class="list" >
<li >
<p ><b >onCreate()</b><span class="Apple-converted-space"></span>This method is called when the provider is started.</p>
</li>
<li >
<p ><b >query()</b><span class="Apple-converted-space"></span>This method receives a request from a client. The result is returned as a Cursor object.</p>
</li>
<li >
<p ><b >insert()</b>This method inserts a new record into the content provider.</p>
</li>
<li >
<p ><b >delete()</b><span class="Apple-converted-space"></span>This method deletes an existing record from the content provider.</p>
</li>
<li >
<p ><b >update()</b><span class="Apple-converted-space"></span>This method updates an existing record from the content provider.</p>
</li>
<li >
<p ><b >getType()</b><span class="Apple-converted-space"></span>This method returns the MIME type of the data at the given URI.</p>
</li>
</ul>
<h2 >Example</h2>
<p >This example will explain you how to create your own<span class="Apple-converted-space"></span><i >ContentProvider</i>. So let's follow the following steps to similar to what we followed while creating<span class="Apple-converted-space"></span><i >Hello World Example</i>−</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th class="fivepct" >Step</th>
<th >Description</th>
</tr>
<tr >
<td >1</td>
<td >You will use Android StudioIDE to create an Android application and name it as<span class="Apple-converted-space"></span><i >My Application</i><span class="Apple-converted-space"></span>under a package<span class="Apple-converted-space"></span><i >com.example.My Application</i>, with blank Activity.</td>
</tr>
<tr >
<td >2</td>
<td >Modify main activity file<span class="Apple-converted-space"></span><i >MainActivity.java</i><span class="Apple-converted-space"></span>to add two new methods<i >onClickAddName()</i><span class="Apple-converted-space"></span>and<span class="Apple-converted-space"></span><i >onClickRetrieveStudents()</i>.</td>
</tr>
<tr >
<td >3</td>
<td >Create a new java file called<span class="Apple-converted-space"></span><i >StudentsProvider.java</i><span class="Apple-converted-space"></span>under the package<i >com.example.My Application</i><span class="Apple-converted-space"></span>to define your actual provider and associated methods.</td>
</tr>
<tr >
<td >4</td>
<td >Register your content provider in your<span class="Apple-converted-space"></span><i >AndroidManifest.xml</i><span class="Apple-converted-space"></span>file using <provider.../> tag</td>
</tr>
<tr >
<td >5</td>
<td >Modify the default content of<span class="Apple-converted-space"></span><i >res/layout/activity_main.xml</i><span class="Apple-converted-space"></span>file to include a small GUI to add students records.</td>
</tr>
<tr >
<td >6</td>
<td >No need to change string.xml.Android studio take care of string.xml file.</td>
</tr>
<tr >
<td >7</td>
<td >Run the application to launch Android emulator and verify the result of the changes done in the application.</td>
</tr>
</tbody>
</table>
<p >Following is the content of the modified main activity file<b >src/com.example.My Application/MainActivity.java</b>. This file can include each of the fundamental life cycle methods. We have added two new methods<i >onClickAddName()</i><span class="Apple-converted-space"></span>and<span class="Apple-converted-space"></span><i >onClickRetrieveStudents()</i><span class="Apple-converted-space"></span>to handle user interaction with the application.</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="typ" >My</span><span class="pln" > </span><span class="typ" >Application</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >net</span><span class="pun" >.</span><span class="typ" >Uri</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >os</span><span class="pun" >.</span><span class="typ" >Bundle</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >Activity</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >ContentValues</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >CursorLoader</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >database</span><span class="pun" >.</span><span class="typ" >Cursor</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >Menu</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >View</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >EditText</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Toast</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MainActivity</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >Activity</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > savedInstanceState</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onCreate</span><span class="pun" >(</span><span class="pln" >savedInstanceState</span><span class="pun" >);</span><span class="pln" >
setContentView</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onCreateOptionsMenu</span><span class="pun" >(</span><span class="typ" >Menu</span><span class="pln" > menu</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
getMenuInflater</span><span class="pun" >().</span><span class="pln" >inflate</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >menu</span><span class="pun" >.</span><span class="pln" >main</span><span class="pun" >,</span><span class="pln" > menu</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClickAddName</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > view</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Add a new student record</span><span class="pln" >
</span><span class="typ" >ContentValues</span><span class="pln" > values </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >ContentValues</span><span class="pun" >();</span><span class="pln" >
values</span><span class="pun" >.</span><span class="pln" >put</span><span class="pun" >(</span><span class="typ" >StudentsProvider</span><span class="pun" >.</span><span class="pln" >NAME</span><span class="pun" >,</span><span class="pln" >
</span><span class="pun" >((</span><span class="typ" >EditText</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >editText2</span><span class="pun" >)).</span><span class="pln" >getText</span><span class="pun" >().</span><span class="pln" >toString</span><span class="pun" >());</span><span class="pln" >
values</span><span class="pun" >.</span><span class="pln" >put</span><span class="pun" >(</span><span class="typ" >StudentsProvider</span><span class="pun" >.</span><span class="pln" >GRADE</span><span class="pun" >,</span><span class="pln" >
</span><span class="pun" >((</span><span class="typ" >EditText</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >editText3</span><span class="pun" >)).</span><span class="pln" >getText</span><span class="pun" >().</span><span class="pln" >toString</span><span class="pun" >());</span><span class="pln" >
</span><span class="typ" >Uri</span><span class="pln" > uri </span><span class="pun" >=</span><span class="pln" > getContentResolver</span><span class="pun" >().</span><span class="pln" >insert</span><span class="pun" >(</span><span class="pln" >
</span><span class="typ" >StudentsProvider</span><span class="pun" >.</span><span class="pln" >CONTENT_URI</span><span class="pun" >,</span><span class="pln" > values</span><span class="pun" >);</span><span class="pln" >
</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="pln" >getBaseContext</span><span class="pun" >(),</span><span class="pln" >
uri</span><span class="pun" >.</span><span class="pln" >toString</span><span class="pun" >(),</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENGTH_LONG</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClickRetrieveStudents</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > view</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >// Retrieve student records</span><span class="pln" >
</span><span class="typ" >String</span><span class="pln" > URL </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"content://com.example.provider.College/students"</span><span class="pun" >;</span><span class="pln" >
</span><span class="typ" >Uri</span><span class="pln" > students </span><span class="pun" >=</span><span class="pln" > </span><span class="typ" >Uri</span><span class="pun" >.</span><span class="pln" >parse</span><span class="pun" >(</span><span class="pln" >URL</span><span class="pun" >);</span><span class="pln" >
</span><span class="typ" >Cursor</span><span class="pln" > c </span><span class="pun" >=</span><span class="pln" > managedQuery</span><span class="pun" >(</span><span class="pln" >students</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >"name"</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >c</span><span class="pun" >.</span><span class="pln" >moveToFirst</span><span class="pun" >())</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >do</span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="pln" >
c</span><span class="pun" >.</span><span class="pln" >getString</span><span class="pun" >(</span><span class="pln" >c</span><span class="pun" >.</span><span class="pln" >getColumnIndex</span><span class="pun" >(</span><span class="typ" >StudentsProvider</span><span class="pun" >.</span><span class="pln" >_ID</span><span class="pun" >))</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" >
</span><span class="str" >", "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > c</span><span class="pun" >.</span><span class="pln" >getString</span><span class="pun" >(</span><span class="pln" >c</span><span class="pun" >.</span><span class="pln" >getColumnIndex</span><span class="pun" >(</span><span class="pln" > </span><span class="typ" >StudentsProvider</span><span class="pun" >.</span><span class="pln" >NAME</span><span class="pun" >))</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" >
</span><span class="str" >", "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > c</span><span class="pun" >.</span><span class="pln" >getString</span><span class="pun" >(</span><span class="pln" >c</span><span class="pun" >.</span><span class="pln" >getColumnIndex</span><span class="pun" >(</span><span class="pln" > </span><span class="typ" >StudentsProvider</span><span class="pun" >.</span><span class="pln" >GRADE</span><span class="pun" >)),</span><span class="pln" >
</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENGTH_SHORT</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >while</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >c</span><span class="pun" >.</span><span class="pln" >moveToNext</span><span class="pun" >());</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >}</span></pre>
<p >Create new file StudentsProvider.java under<span class="Apple-converted-space"></span><i >com.example.My Application</i>package and following is the content of<span class="Apple-converted-space"></span><b >src/com.example.My Application/StudentsProvider.java</b><span class="Apple-converted-space"></span>−</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="typ" >My</span><span class="pln" > </span><span class="typ" >Application</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >util</span><span class="pun" >.</span><span class="typ" >HashMap</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >ContentProvider</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >ContentUris</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >ContentValues</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >Context</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >UriMatcher</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >database</span><span class="pun" >.</span><span class="typ" >Cursor</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >database</span><span class="pun" >.</span><span class="typ" >SQLException</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >database</span><span class="pun" >.</span><span class="pln" >sqlite</span><span class="pun" >.</span><span class="typ" >SQLiteDatabase</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >database</span><span class="pun" >.</span><span class="pln" >sqlite</span><span class="pun" >.</span><span class="typ" >SQLiteOpenHelper</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >database</span><span class="pun" >.</span><span class="pln" >sqlite</span><span class="pun" >.</span><span class="typ" >SQLiteQueryBuilder</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >net</span><span class="pun" >.</span><span class="typ" >Uri</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >text</span><span class="pun" >.</span><span class="typ" >TextUtils</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >StudentsProvider</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >ContentProvider</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > PROVIDER_NAME </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"com.example.provider.College"</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > URL </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"content://"</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > PROVIDER_NAME </span><span class="pun" >+</span><span class="pln" > </span><span class="str" >"/students"</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >Uri</span><span class="pln" > CONTENT_URI </span><span class="pun" >=</span><span class="pln" > </span><span class="typ" >Uri</span><span class="pun" >.</span><span class="pln" >parse</span><span class="pun" >(</span><span class="pln" >URL</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > _ID </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"_id"</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > NAME </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"name"</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > GRADE </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"grade"</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="kwd" >static</span><span class="pln" > </span><span class="typ" >HashMap</span><span class="pun" ><</span><span class="typ" >String</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >String</span><span class="pun" >></span><span class="pln" > STUDENTS_PROJECTION_MAP</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > STUDENTS </span><span class="pun" >=</span><span class="pln" > </span><span class="lit" >1</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > STUDENT_ID </span><span class="pun" >=</span><span class="pln" > </span><span class="lit" >2</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >UriMatcher</span><span class="pln" > uriMatcher</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pun" >{</span><span class="pln" >
uriMatcher </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >UriMatcher</span><span class="pun" >(</span><span class="typ" >UriMatcher</span><span class="pun" >.</span><span class="pln" >NO_MATCH</span><span class="pun" >);</span><span class="pln" >
uriMatcher</span><span class="pun" >.</span><span class="pln" >addURI</span><span class="pun" >(</span><span class="pln" >PROVIDER_NAME</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >"students"</span><span class="pun" >,</span><span class="pln" > STUDENTS</span><span class="pun" >);</span><span class="pln" >
uriMatcher</span><span class="pun" >.</span><span class="pln" >addURI</span><span class="pun" >(</span><span class="pln" >PROVIDER_NAME</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >"students/#"</span><span class="pun" >,</span><span class="pln" > STUDENT_ID</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="com" >/**
* Database specific constant declarations
*/</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >SQLiteDatabase</span><span class="pln" > db</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > DATABASE_NAME </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"College"</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > STUDENTS_TABLE_NAME </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"students"</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > DATABASE_VERSION </span><span class="pun" >=</span><span class="pln" > </span><span class="lit" >1</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > CREATE_DB_TABLE </span><span class="pun" >=</span><span class="pln" >
</span><span class="str" >" CREATE TABLE "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > STUDENTS_TABLE_NAME </span><span class="pun" >+</span><span class="pln" >
</span><span class="str" >" (_id INTEGER PRIMARY KEY AUTOINCREMENT, "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" >
</span><span class="str" >" name TEXT NOT NULL, "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" >
</span><span class="str" >" grade TEXT NOT NULL);"</span><span class="pun" >;</span><span class="pln" >
</span><span class="com" >/**
* Helper class that actually creates and manages
* the provider's underlying data repository.
*/</span><span class="pln" >
</span><span class="kwd" >private</span><span class="pln" > </span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >DatabaseHelper</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >SQLiteOpenHelper</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >DatabaseHelper</span><span class="pun" >(</span><span class="typ" >Context</span><span class="pln" > context</span><span class="pun" >){</span><span class="pln" >
</span><span class="kwd" >super</span><span class="pun" >(</span><span class="pln" >context</span><span class="pun" >,</span><span class="pln" > DATABASE_NAME</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >,</span><span class="pln" > DATABASE_VERSION</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >SQLiteDatabase</span><span class="pln" > db</span><span class="pun" >)</span><span class="pln" >
</span><span class="pun" >{</span><span class="pln" >
db</span><span class="pun" >.</span><span class="pln" >execSQL</span><span class="pun" >(</span><span class="pln" >CREATE_DB_TABLE</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onUpgrade</span><span class="pun" >(</span><span class="typ" >SQLiteDatabase</span><span class="pln" > db</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > oldVersion</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > newVersion</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
db</span><span class="pun" >.</span><span class="pln" >execSQL</span><span class="pun" >(</span><span class="str" >"DROP TABLE IF EXISTS "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > STUDENTS_TABLE_NAME</span><span class="pun" >);</span><span class="pln" >
onCreate</span><span class="pun" >(</span><span class="pln" >db</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onCreate</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >Context</span><span class="pln" > context </span><span class="pun" >=</span><span class="pln" > getContext</span><span class="pun" >();</span><span class="pln" >
</span><span class="typ" >DatabaseHelper</span><span class="pln" > dbHelper </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >DatabaseHelper</span><span class="pun" >(</span><span class="pln" >context</span><span class="pun" >);</span><span class="pln" >
</span><span class="com" >/**
* Create a write able database which will trigger its
* creation if it doesn't already exist.
*/</span><span class="pln" >
db </span><span class="pun" >=</span><span class="pln" > dbHelper</span><span class="pun" >.</span><span class="pln" >getWritableDatabase</span><span class="pun" >();</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >db </span><span class="pun" >==</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >)?</span><span class="pln" > </span><span class="kwd" >false</span><span class="pun" >:</span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="typ" >Uri</span><span class="pln" > insert</span><span class="pun" >(</span><span class="typ" >Uri</span><span class="pln" > uri</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >ContentValues</span><span class="pln" > values</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="com" >/**
* Add a new student record
*/</span><span class="pln" >
</span><span class="kwd" >long</span><span class="pln" > rowID </span><span class="pun" >=</span><span class="pln" > db</span><span class="pun" >.</span><span class="pln" >insert</span><span class="pun" >(</span><span class="pln" > STUDENTS_TABLE_NAME</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >""</span><span class="pun" >,</span><span class="pln" > values</span><span class="pun" >);</span><span class="pln" >
</span><span class="com" >/**
* If record is added successfully
*/</span><span class="pln" >
</span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >rowID </span><span class="pun" >></span><span class="pln" > </span><span class="lit" >0</span><span class="pun" >)</span><span class="pln" >
</span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >Uri</span><span class="pln" > _uri </span><span class="pun" >=</span><span class="pln" > </span><span class="typ" >ContentUris</span><span class="pun" >.</span><span class="pln" >withAppendedId</span><span class="pun" >(</span><span class="pln" >CONTENT_URI</span><span class="pun" >,</span><span class="pln" > rowID</span><span class="pun" >);</span><span class="pln" >
getContext</span><span class="pun" >().</span><span class="pln" >getContentResolver</span><span class="pun" >().</span><span class="pln" >notifyChange</span><span class="pun" >(</span><span class="pln" >_uri</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > _uri</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >throw</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >SQLException</span><span class="pun" >(</span><span class="str" >"Failed to add a record into "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > uri</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="typ" >Cursor</span><span class="pln" > query</span><span class="pun" >(</span><span class="typ" >Uri</span><span class="pln" > uri</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >String</span><span class="pun" >[]</span><span class="pln" > projection</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > selection</span><span class="pun" >,</span><span class="typ" >String</span><span class="pun" >[]</span><span class="pln" > selectionArgs</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > sortOrder</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >SQLiteQueryBuilder</span><span class="pln" > qb </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >SQLiteQueryBuilder</span><span class="pun" >();</span><span class="pln" >
qb</span><span class="pun" >.</span><span class="pln" >setTables</span><span class="pun" >(</span><span class="pln" >STUDENTS_TABLE_NAME</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >switch</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >uriMatcher</span><span class="pun" >.</span><span class="pln" >match</span><span class="pun" >(</span><span class="pln" >uri</span><span class="pun" >))</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > STUDENTS</span><span class="pun" >:</span><span class="pln" >
qb</span><span class="pun" >.</span><span class="pln" >setProjectionMap</span><span class="pun" >(</span><span class="pln" >STUDENTS_PROJECTION_MAP</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > STUDENT_ID</span><span class="pun" >:</span><span class="pln" >
qb</span><span class="pun" >.</span><span class="pln" >appendWhere</span><span class="pun" >(</span><span class="pln" > _ID </span><span class="pun" >+</span><span class="pln" > </span><span class="str" >"="</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > uri</span><span class="pun" >.</span><span class="pln" >getPathSegments</span><span class="pun" >().</span><span class="kwd" >get</span><span class="pun" >(</span><span class="lit" >1</span><span class="pun" >));</span><span class="pln" >
</span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >default</span><span class="pun" >:</span><span class="pln" >
</span><span class="kwd" >throw</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >IllegalArgumentException</span><span class="pun" >(</span><span class="str" >"Unknown URI "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > uri</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >sortOrder </span><span class="pun" >==</span><span class="pln" > </span><span class="kwd" >null</span><span class="pln" > </span><span class="pun" >||</span><span class="pln" > sortOrder </span><span class="pun" >==</span><span class="pln" > </span><span class="str" >""</span><span class="pun" >){</span><span class="pln" >
</span><span class="com" >/**
* By default sort on student names
*/</span><span class="pln" >
sortOrder </span><span class="pun" >=</span><span class="pln" > NAME</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="typ" >Cursor</span><span class="pln" > c </span><span class="pun" >=</span><span class="pln" > qb</span><span class="pun" >.</span><span class="pln" >query</span><span class="pun" >(</span><span class="pln" >db</span><span class="pun" >,</span><span class="pln" > projection</span><span class="pun" >,</span><span class="pln" > selection</span><span class="pun" >,</span><span class="pln" > selectionArgs</span><span class="pun" >,</span><span class="kwd" >null</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >,</span><span class="pln" > sortOrder</span><span class="pun" >);</span><span class="pln" >
</span><span class="com" >/**
* register to watch a content URI for changes
*/</span><span class="pln" >
c</span><span class="pun" >.</span><span class="pln" >setNotificationUri</span><span class="pun" >(</span><span class="pln" >getContext</span><span class="pun" >().</span><span class="pln" >getContentResolver</span><span class="pun" >(),</span><span class="pln" > uri</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > c</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > </span><span class="kwd" >delete</span><span class="pun" >(</span><span class="typ" >Uri</span><span class="pln" > uri</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > selection</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >String</span><span class="pun" >[]</span><span class="pln" > selectionArgs</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >int</span><span class="pln" > count </span><span class="pun" >=</span><span class="pln" > </span><span class="lit" >0</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >switch</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >uriMatcher</span><span class="pun" >.</span><span class="pln" >match</span><span class="pun" >(</span><span class="pln" >uri</span><span class="pun" >)){</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > STUDENTS</span><span class="pun" >:</span><span class="pln" >
count </span><span class="pun" >=</span><span class="pln" > db</span><span class="pun" >.</span><span class="kwd" >delete</span><span class="pun" >(</span><span class="pln" >STUDENTS_TABLE_NAME</span><span class="pun" >,</span><span class="pln" > selection</span><span class="pun" >,</span><span class="pln" > selectionArgs</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > STUDENT_ID</span><span class="pun" >:</span><span class="pln" >
</span><span class="typ" >String</span><span class="pln" > id </span><span class="pun" >=</span><span class="pln" > uri</span><span class="pun" >.</span><span class="pln" >getPathSegments</span><span class="pun" >().</span><span class="kwd" >get</span><span class="pun" >(</span><span class="lit" >1</span><span class="pun" >);</span><span class="pln" >
count </span><span class="pun" >=</span><span class="pln" > db</span><span class="pun" >.</span><span class="kwd" >delete</span><span class="pun" >(</span><span class="pln" > STUDENTS_TABLE_NAME</span><span class="pun" >,</span><span class="pln" > _ID </span><span class="pun" >+</span><span class="pln" > </span><span class="str" >" = "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > id </span><span class="pun" >+</span><span class="pln" >
</span><span class="pun" >(!</span><span class="typ" >TextUtils</span><span class="pun" >.</span><span class="pln" >isEmpty</span><span class="pun" >(</span><span class="pln" >selection</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >?</span><span class="pln" > </span><span class="str" >" AND ("</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > selection </span><span class="pun" >+</span><span class="pln" > </span><span class="str" >')'</span><span class="pln" > </span><span class="pun" >:</span><span class="pln" > </span><span class="str" >""</span><span class="pun" >),</span><span class="pln" > selectionArgs</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >default</span><span class="pun" >:</span><span class="pln" >
</span><span class="kwd" >throw</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >IllegalArgumentException</span><span class="pun" >(</span><span class="str" >"Unknown URI "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > uri</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
getContext</span><span class="pun" >().</span><span class="pln" >getContentResolver</span><span class="pun" >().</span><span class="pln" >notifyChange</span><span class="pun" >(</span><span class="pln" >uri</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > count</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > update</span><span class="pun" >(</span><span class="typ" >Uri</span><span class="pln" > uri</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >ContentValues</span><span class="pln" > values</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > selection</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >String</span><span class="pun" >[]</span><span class="pln" > selectionArgs</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >int</span><span class="pln" > count </span><span class="pun" >=</span><span class="pln" > </span><span class="lit" >0</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >switch</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >uriMatcher</span><span class="pun" >.</span><span class="pln" >match</span><span class="pun" >(</span><span class="pln" >uri</span><span class="pun" >)){</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > STUDENTS</span><span class="pun" >:</span><span class="pln" >
count </span><span class="pun" >=</span><span class="pln" > db</span><span class="pun" >.</span><span class="pln" >update</span><span class="pun" >(</span><span class="pln" >STUDENTS_TABLE_NAME</span><span class="pun" >,</span><span class="pln" > values</span><span class="pun" >,</span><span class="pln" > selection</span><span class="pun" >,</span><span class="pln" > selectionArgs</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > STUDENT_ID</span><span class="pun" >:</span><span class="pln" >
count </span><span class="pun" >=</span><span class="pln" > db</span><span class="pun" >.</span><span class="pln" >update</span><span class="pun" >(</span><span class="pln" >STUDENTS_TABLE_NAME</span><span class="pun" >,</span><span class="pln" > values</span><span class="pun" >,</span><span class="pln" > _ID </span><span class="pun" >+</span><span class="pln" > </span><span class="str" >" = "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > uri</span><span class="pun" >.</span><span class="pln" >getPathSegments</span><span class="pun" >().</span><span class="kwd" >get</span><span class="pun" >(</span><span class="lit" >1</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" >
</span><span class="pun" >(!</span><span class="typ" >TextUtils</span><span class="pun" >.</span><span class="pln" >isEmpty</span><span class="pun" >(</span><span class="pln" >selection</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >?</span><span class="pln" > </span><span class="str" >" AND ("</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" >selection </span><span class="pun" >+</span><span class="pln" > </span><span class="str" >')'</span><span class="pln" > </span><span class="pun" >:</span><span class="pln" > </span><span class="str" >""</span><span class="pun" >),</span><span class="pln" > selectionArgs</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >default</span><span class="pun" >:</span><span class="pln" >
</span><span class="kwd" >throw</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >IllegalArgumentException</span><span class="pun" >(</span><span class="str" >"Unknown URI "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > uri </span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
getContext</span><span class="pun" >().</span><span class="pln" >getContentResolver</span><span class="pun" >().</span><span class="pln" >notifyChange</span><span class="pun" >(</span><span class="pln" >uri</span><span class="pun" >,</span><span class="pln" > </span><span class="kwd" >null</span><span class="pun" >);</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > count</span><span class="pun" >;</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > getType</span><span class="pun" >(</span><span class="typ" >Uri</span><span class="pln" > uri</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >switch</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >uriMatcher</span><span class="pun" >.</span><span class="pln" >match</span><span class="pun" >(</span><span class="pln" >uri</span><span class="pun" >)){</span><span class="pln" >
</span><span class="com" >/**
* Get all student records
*/</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > STUDENTS</span><span class="pun" >:</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="str" >"vnd.android.cursor.dir/vnd.example.students"</span><span class="pun" >;</span><span class="pln" >
</span><span class="com" >/**
* Get a particular student
*/</span><span class="pln" >
</span><span class="kwd" >case</span><span class="pln" > STUDENT_ID</span><span class="pun" >:</span><span class="pln" >
</span><span class="kwd" >return</span><span class="pln" > </span><span class="str" >"vnd.android.cursor.item/vnd.example.students"</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >default</span><span class="pun" >:</span><span class="pln" >
</span><span class="kwd" >throw</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >IllegalArgumentException</span><span class="pun" >(</span><span class="str" >"Unsupported URI: "</span><span class="pln" > </span><span class="pun" >+</span><span class="pln" > uri</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >}</span></pre>
<p >Following will the modified content of<span class="Apple-converted-space"></span><i >AndroidManifest.xml</i><span class="Apple-converted-space"></span>file. Here we have added <provider.../> tag to include our content provider:</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pun" ><?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >"1.0"</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >"utf-8"</span><span class="pun" >?></span><span class="pln" >
</span><span class="tag" ><manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/apk/res/android"</span><span class="pln" >
</span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >"com.example.My Application"</span><span class="pln" >
</span><span class="atn" >android:versionCode</span><span class="pun" >=</span><span class="atv" >"1"</span><span class="pln" >
</span><span class="atn" >android:versionName</span><span class="pun" >=</span><span class="atv" >"1.0"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><uses-sdk</span><span class="pln" >
</span><span class="atn" >android:minSdkVersion</span><span class="pun" >=</span><span class="atv" >"8"</span><span class="pln" >
</span><span class="atn" >android:targetSdkVersion</span><span class="pun" >=</span><span class="atv" >"22"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><application</span><span class="pln" >
</span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >"@drawable/ic_launcher"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" >
</span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >"@style/AppTheme"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><activity</span><span class="pln" >
</span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"com.example.My Application.MainActivity"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><intent-filter></span><span class="pln" >
</span><span class="tag" ><action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.action.MAIN"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.category.LAUNCHER"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></intent-filter></span><span class="pln" >
</span><span class="tag" ></activity></span><span class="pln" >
<provider android:name="StudentsProvider"
</span><span class="tag" ><android:authorities</span><span class="pun" >=</span><span class="atv" >"com.example.provider.College"</span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ></provider></span><span class="pln" >
</span><span class="tag" ></application></span><span class="pln" >
</span><span class="tag" ></manifest></span></pre>
<p >Following will be the content of<span class="Apple-converted-space"></span><b >res/layout/activity_main.xml</b><span class="Apple-converted-space"></span>file to include a button to broadcast your custom intent −</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="tag" ><RelativeLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/apk/res/android"</span><span class="pln" >
</span><span class="atn" >xmlns:tools</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/tools"</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"match_parent"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"match_parent"</span><span class="pln" > </span><span class="atn" >android:paddingLeft</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_horizontal_margin"</span><span class="pln" >
</span><span class="atn" >android:paddingRight</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_horizontal_margin"</span><span class="pln" >
</span><span class="atn" >android:paddingTop</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_vertical_margin"</span><span class="pln" >
</span><span class="atn" >android:paddingBottom</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_vertical_margin"</span><span class="pln" > </span><span class="atn" >tools:context</span><span class="pun" >=</span><span class="atv" >".MainActivity"</span><span class="pun" >/</span><span class="atv" >"></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/textView1"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Content provider"</span><span class="pln" >
</span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >"30dp"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Tutorials point "</span><span class="pln" >
</span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >"#ff87ff09"</span><span class="pln" >
</span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >"30dp"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/textView1"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><ImageButton</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" >
</span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >"@drawable/abc"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><Button</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/button2"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Add Name"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/editText3"</span><span class="pln" >
</span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignLeft</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignStart</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:onClick</span><span class="pun" >=</span><span class="atv" >"onClickAddName"</span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><EditText</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/editText"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" >
</span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" >
</span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >"@+id/imageButton"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><EditText</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/editText2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignTop</span><span class="pun" >=</span><span class="atv" >"@+id/editText"</span><span class="pln" >
</span><span class="atn" >android:layout_alignLeft</span><span class="pun" >=</span><span class="atv" >"@+id/textView1"</span><span class="pln" >
</span><span class="atn" >android:layout_alignStart</span><span class="pun" >=</span><span class="atv" >"@+id/textView1"</span><span class="pln" >
</span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >"@+id/textView1"</span><span class="pln" >
</span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >"@+id/textView1"</span><span class="pln" >
</span><span class="atn" >android:hint</span><span class="pun" >=</span><span class="atv" >"Name"</span><span class="pln" >
</span><span class="atn" >android:textColorHint</span><span class="pun" >=</span><span class="atv" >"@android:color/holo_blue_light"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><EditText</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/editText3"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/editText"</span><span class="pln" >
</span><span class="atn" >android:layout_alignLeft</span><span class="pun" >=</span><span class="atv" >"@+id/editText2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignStart</span><span class="pun" >=</span><span class="atv" >"@+id/editText2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >"@+id/editText2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >"@+id/editText2"</span><span class="pln" >
</span><span class="atn" >android:hint</span><span class="pun" >=</span><span class="atv" >"Grade"</span><span class="pln" >
</span><span class="atn" >android:textColorHint</span><span class="pun" >=</span><span class="atv" >"@android:color/holo_blue_bright"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><Button</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Retrive student"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/button"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/button2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >"@+id/editText3"</span><span class="pln" >
</span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >"@+id/editText3"</span><span class="pln" >
</span><span class="atn" >android:layout_alignLeft</span><span class="pun" >=</span><span class="atv" >"@+id/button2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignStart</span><span class="pun" >=</span><span class="atv" >"@+id/button2"</span><span class="pln" >
</span><span class="atn" >android:onClick</span><span class="pun" >=</span><span class="atv" >"onClickRetrieveStudents"</span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></RelativeLayout></span></pre>
<p >Make sure you have following content of<span class="Apple-converted-space"></span><b >res/values/strings.xml</b><span class="Apple-converted-space"></span>file:</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pun" ><?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >"1.0"</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >"utf-8"</span><span class="pun" >?></span><span class="pln" >
</span><span class="tag" ><resources></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"app_name"</span><span class="tag" >></span><span class="pln" >My Application</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"action_settings"</span><span class="tag" >></span><span class="pln" >Settings</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ></resources></span><span class="pln" >;</span></pre>
<p >Let's try to run our modified<span class="Apple-converted-space"></span><b >My Application</b><span class="Apple-converted-space"></span>application we just created. I assume you had created your<span class="Apple-converted-space"></span><b >AVD</b><span class="Apple-converted-space"></span>while doing environment set-up. To run the app from Android Studio IDE, open one of your project's activity files and click Run<span class="Apple-converted-space"></span><img alt="Android StudioRun Icon" class="inline" src="http://www.tutorialspoint.com/android/images/eclipse_run.jpg" /><span class="Apple-converted-space"></span>icon from the tool bar. Android Studio installs the app on your AVD and starts it and if everything is fine with your set-up and application, it will display following Emulator window, be patience because it may take sometime based on your computer speed:</p>
<p><img alt="Android Content Provider Demo" src="http://www.tutorialspoint.com/android/images/content2.jpg" /></p>
<p >Now let's enter student<span class="Apple-converted-space"></span><b >Name</b><span class="Apple-converted-space"></span>and<span class="Apple-converted-space"></span><b >Grade</b><span class="Apple-converted-space"></span>and finally click on<span class="Apple-converted-space"></span><b >Add Name</b>button, this will add student record in the database and will flash a message at the bottom showing ContentProvider URI along with record number added in the database. This operation makes use of our<span class="Apple-converted-space"></span><b >insert()</b><span class="Apple-converted-space"></span>method. Let's repeat this process to add few more students in the database of our content provider.</p>
<p><img alt="Add Records using ContentProvider" src="http://www.tutorialspoint.com/android/images/content3.jpg" /></p>
<p >Once you are done with adding records in the database, now its time to ask ContentProvider to give us those records back, so let's click<span class="Apple-converted-space"></span><b >Retrieve Students</b><span class="Apple-converted-space"></span>button which will fetch and display all the records one by one which is as per our the implementation of our<span class="Apple-converted-space"></span><b >query()</b><span class="Apple-converted-space"></span>method.</p>
<p >You can write activities against update and delete operations by providing callback functions in<span class="Apple-converted-space"></span><b >MainActivity.java</b><span class="Apple-converted-space"></span>file and then modify user interface to have buttons for update and deleted operations in the same way as we have done for add and read operations.</p>
<p >This way you can use existing Content Provider like Address Book or you can use Content Provider concept in developing nice database oriented applications where you can perform all sort of database operations like read, write, update and delete as explained above in the example.</p>