android--creating your own content Provider

Source: Internet
Author: User

To implement cross-program shared data requires a content provider that provides access to other applications, and creates a new class to inherit ContentProvider to create a content provider of its own. There are 6 abstract methods in the ContentProvider class that need to be rewritten when using subclass inheritance.

1  Public classMyproviderextendsContentProvider {2 3 @Override4     /*5 called when initializing6 operations such as creating and upgrading the database are usually done here, and returning TRUE indicates success in initialization. 7 initialization occurs only if there is a contentresolver attempt to access data in the program. 8      */9      Public BooleanonCreate () {Ten         return false; One     } A  -     /* - Querying Data the URI determines which table is queried - projection query that column - selection and Selectionargs query those rows - SortOrder to sort results + The returned data is stored in the cursor object -      */ + @Override A      PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { at         return NULL; -     } -  -     /* - returns the corresponding MIME type according to the data URI passed in -      */ in @Override -      PublicString getType (Uri uri) { to         return NULL; +     } -     /* the Add Data * The URI determines which table is added $ values Save the data you want to addPanax Notoginseng returns a URI that represents the new record -      */ the @Override +      Publicuri insert (URI uri, contentvalues values) { A         return NULL; the     } +     /* - Delete Data $ selection and Selectionargs delete those rows $ returns the number of rows that were deleted -      */ - @Override the      Public intDelete (Uri Uri, String selection, string[] selectionargs) { -         return0;Wuyi     } the     /* - Update existing Data Wu URI determines which table is updated - Values New Data About selection and Selectionargs update those lines $ returns the number of rows updated -      */ - @Override -      Public intupdate (URI Uri, contentvalues values, String selection, string[] selectionargs) { A         return0; +     } the  -}

A standard content URI notation is this:

Content://com.example.app.providder/table1

This means that you want to access the data in the Table1 table in the Com.example.app application

Content://com.example.app.providder/table1/1

This means that you want to access data with ID 1 in the Table1 table in the Com.example.app application.

content://com.example.app.providder/*

This means that you want to access any of the tables in this app Com.example.app

content://com.example.app.providder/table1/#

This means that you want to access data from any row in the Table1 table in the Com.example.app application.

where * represents any length of any character # represents any length of the number

Then use urimatcher this class to implement the function of matching content URI, there is a method of Adduri () in Urimatcher, 3 parameters, can be authority, path, and a custom code respectively, when calling Urimatcher's match ( method is to pass in a URI that returns a custom code that matches the URI.

The Modified code:

  

1  Public classMyproviderextendsContentProvider {2 3     //custom code to identify the URI4      Public Static Final intTable1_dir = 0;5 6      Public Static Final intTable1_item = 1;7 8      Public Static Final intTable2_dir = 2;9 Ten      Public Static Final intTable2_item = 3; One  A     //Create an instance of Urimatcher -     Private StaticUrimatcher Urimatcher; -  the     //match the custom code with the desired URI -     Static { -Urimatcher =NewUrimatcher (urimatcher.no_match); -Urimatcher.adduri ("Xbt.exp11.provider", "table1", Table1_dir); +Urimatcher.adduri ("Xbt.exp11.provider", "table1/#", Table1_item); -Urimatcher.adduri ("Xbt.exp11.provider", "Table2", Table2_dir); +Urimatcher.adduri ("Xbt.exp11.provider", "table2/#", Table2_item); A  at     } -     ... -     /* - Querying Data - URI determines which table is queried - projection query that column in selection and Selectionargs query those rows - SortOrder to sort results to The returned data is stored in the cursor object +      */ - @Override the      PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { *  $         //determine what data is expected to be accessedPanax Notoginseng         Switch(Urimatcher.match (URI)) { -              CaseTable1_dir: the                 //querying all data in a table1 table +                  Break; A              CaseTable1_item: the                 //querying a single piece of data in a table1 table +                  Break; -              CaseTable2_dir: $                 //querying all data in a table2 table $                  Break; -              CaseTable2_item: -                 //querying a single piece of data in a table2 table the                  Break; -             default:Wuyi                  Break; the         } -         ... Wu     } -  About     ... $  -}

Insert (), update (), delete () can also use switch (Urimatcher.match (URI)) {:} Such a judgment sentence to determine the desired access to the table, and then the corresponding operation

The GetType () method is a method that all content providers must provide to get the MIME type that the URI object corresponds to.

A content URI that corresponds to a MIME string consists mainly of 3 parts, and the android format specifies:

1. Must start with VND

2. If the content URI ends with a path, then android.cursor.dir/is followed, and if the content URI ends with an ID, then the android.cursor.item/is followed up

3, finally connected to the Vnd.<authority>.<path>

Therefore, for content://com.example.app.providder/table1 this content URI, its corresponding MIME type can be written as:

Vnd.android.cursor.dir/vnd.example.app.providder.table1

For CONTENT://COM.EXAMPLE.APP.PROVIDDER/TABLE1/1 This content URI, the MIME type it corresponds to can be written as:

Vnd.android.cursor. Item/vnd.example.app.providder.table1

The logic for implementing the GetType () method is as follows:

 Public classMyproviderextendsContentProvider {.../*returns the corresponding MIME type according to the data URI passed in*/@Override PublicString getType (Uri uri) {Switch(Urimatcher.match (URI)) { CaseTable1_dir:return"Vnd.android.cursor.dir/vnd.xbt.exp11.provider.table1";  CaseTable1_item:return"Vnd.android.cursor.item/vnd.xbt.exp11.provider.table1";  CaseTable2_dir:return"Vnd.android.cursor.dir/vnd.xbt.exp11.provider.table2";  CaseTable2_item:return"Vnd.android.cursor.item/vnd.xbt.exp11.provider.table2"; default:                 Break; }        return NULL; }}

android--creating your own 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.