Implementation of the Android content provider

Source: Internet
Author: User

Then the above "Android content provider profile" is further implemented by content providers.

Each content provider class uses a URI (Universal Resource Identifier, Universal Resource Identifier) as a separate identity, in the form of: content://com.example.app.provider/ Table1. Other applications access different content providers through different URIs and get/manipulate the data inside.

For example, in this project, it corresponds to the following URI:

Actions added by Content://com.wuyudong.db.personprovider/insert
Content://com.wuyudong.db.personprovider/delete deleted actions
Content://com.wuyudong.db.personprovider/update Updated operations
Content://com.wuyudong.db.personprovider/query the operation of the query

To add code in Persondbprovider.java:

 Packagecom.wuyudong.db;ImportAndroid.content.ContentProvider;Importandroid.content.ContentValues;ImportAndroid.content.UriMatcher;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.net.Uri; Public classPersondbproviderextendsContentProvider {//defines a URI that matches the URI to match if the path does not meet the conditional return-1    Private StaticUrimatcher Matcher =NewUrimatcher (Urimatcher.no_match); Private Static Final intINSERT = 1; Private Static Final intDELETE = 2; Private Static Final intUPDATE = 3; Private Static Final intQUERY = 4; PrivatePersonsqliteopenhelper Helper; Static {        //add a set of matching rules Com.wuyudong.db.personproviderMatcher.adduri ("Com.wuyudong.db.personprovider", "Insert", INSERT); Matcher.adduri ("Com.wuyudong.db.personprovider", "delete", DELETE); Matcher.adduri ("Com.wuyudong.db.personprovider", "Update", UPDATE); Matcher.adduri ("Com.wuyudong.db.personprovider", "Query", QUERY); } @Override Public BooleanonCreate () {helper=NewPersonsqliteopenhelper (GetContext ()); return false; } @Override PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { if(Matcher.match (uri) = =QUERY) {            //returns the result set of the querySqlitedatabase db =helper.getreadabledatabase (); Cursor Cursor= Db.query ("person", projection, selection, Selectionargs,NULL,NULL, SortOrder); returncursor; } Else {            Throw NewIllegalArgumentException ("Path mismatch, cannot perform query operation"); }} @Override PublicString getType (Uri uri) {//TODO auto-generated Method Stub        return NULL; } @Override Publicuri insert (URI uri, contentvalues values) {//TODO auto-generated Method Stub        return NULL; } @Override Public intDelete (Uri Uri, String selection, string[] selectionargs) {//TODO auto-generated Method Stub        return0; } @Override Public intupdate (URI Uri, contentvalues values, String selection, string[] selectionargs) {//TODO auto-generated Method Stub        return0; }}

Create a new project named other with the following layout:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " >    <ButtonAndroid:onclick= "click"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "read data from DB" /></Relativelayout>

Click on the button to kill the com.wuyudong.db process

When you click the button, the com.wuyudong.db process runs again, and the data in the person table in the database is printed

Further refine other operations, modify layouts

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <ButtonAndroid:onclick= "click"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "read data from DB" />     <ButtonAndroid:onclick= "Delete"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Delete data from DB" />      <ButtonAndroid:onclick= "Update"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Modify data for db" />       <ButtonAndroid:onclick= "Insert"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Add DB data" /></LinearLayout>

Next implement the other methods in Persondbprovider

 Packagecom.wuyudong.db;ImportAndroid.content.ContentProvider;Importandroid.content.ContentValues;ImportAndroid.content.UriMatcher;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.net.Uri; Public classPersondbproviderextendsContentProvider {//defines a URI that matches the URI to match if the path does not meet the conditional return-1    Private StaticUrimatcher Matcher =NewUrimatcher (Urimatcher.no_match); Private Static Final intINSERT = 1; Private Static Final intDELETE = 2; Private Static Final intUPDATE = 3; Private Static Final intQUERY = 4; PrivatePersonsqliteopenhelper Helper; Static {        //add a set of matching rules Com.wuyudong.db.personproviderMatcher.adduri ("Com.wuyudong.db.personprovider", "Insert", INSERT); Matcher.adduri ("Com.wuyudong.db.personprovider", "delete", DELETE); Matcher.adduri ("Com.wuyudong.db.personprovider", "Update", UPDATE); Matcher.adduri ("Com.wuyudong.db.personprovider", "Query", QUERY); } @Override Public BooleanonCreate () {helper=NewPersonsqliteopenhelper (GetContext ()); return false; } @Override PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { if(Matcher.match (uri) = =QUERY) {            //returns the result set of the querySqlitedatabase db =helper.getreadabledatabase (); Cursor Cursor= Db.query ("Person", projection, selection, Selectionargs,NULL,NULL, SortOrder); returncursor; } Else {            Throw NewIllegalArgumentException ("Path mismatch, cannot perform query operation"); }} @Override PublicString getType (Uri uri) {//TODO auto-generated Method Stub        return NULL; } @Override Publicuri insert (URI uri, contentvalues values) {if(Matcher.match (uri) = =INSERT) {            //returns the result set of the querySqlitedatabase db =helper.getwritabledatabase (); Db.insert ("Person",NULL, values); } Else {            Throw NewIllegalArgumentException ("Path mismatch, cannot perform insert operation"); }        return NULL; } @Override Public intDelete (Uri Uri, String selection, string[] selectionargs) {if(Matcher.match (uri) = =DELETE) {            //returns the result set of the querySqlitedatabase db =helper.getwritabledatabase (); Db.delete ("Person", Selection, Selectionargs); } Else {            Throw NewIllegalArgumentException ("Path mismatch, cannot perform delete operation"); }        return0; } @Override Public intupdate (URI Uri, contentvalues values, String selection, string[] selectionargs) {if(Matcher.match (uri) = =UPDATE) {            //returns the result set of the querySqlitedatabase db =helper.getwritabledatabase (); Db.update ("Person", values, selection, Selectionargs); } Else {            Throw NewIllegalArgumentException ("Path mismatch, cannot perform modify operation"); }        return0; }}

Implementation of the Android content provider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.