Simple use of ContentProvider in Android

Source: Internet
Author: User

1. New class for inheriting ContentProvider

 PackageCom.wangzhu.demo;ImportAndroid.content.ContentProvider;Importandroid.content.ContentValues;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.net.Uri; Public classMycontentproviderextendsContentProvider {/*** ContentProvider data access Path*/     Public Static FinalUri uri = uri.parse ("Content://com.wangzhu.provider"); Privatesqlitedatabase database; @Override Public intDelete (Uri arg0, String arg1, string[] arg2) {return0; } @Override PublicString GetType (Uri arg0) {return NULL; } @Override Publicuri insert (URI uri, contentvalues values) {Database.insert ("tab", "_id", values); return NULL; } @Override Public BooleanonCreate () {database= GetContext (). Openorcreatedatabase ("Myprovider.db3", Context.mode_private,NULL); Database.execsql ("CREATE TABLE IF not EXISTS tab (_id INTEGER PRIMARY KEY autoincrement,name TEXT not NULL)"); return true; } @Override Publiccursor Query (Uri arg0, string[] arg1, String arg2, string[] arg3, string arg4) {cursor cursor = Database.query ("tab",NULL,NULL,NULL,NULL,NULL,                NULL); returncursor; } @Override Public intUpdate (Uri arg0, contentvalues arg1, String arg2, string[] arg3) {return0; }}

2. Add ContentProvider access to the application in Androidmanifest.xml

        <!--android:exported is set to true to indicate that other apps can access it, otherwise it can't be accessed-        <provider            android:name= " Mycontentprovider "            android:authorities=" Com.wangzhu.provider "            android:exported=" true ">        </provider>

3. Insert and Query

        FinalUri uri =Mycontentprovider.uri; /*** Write data to the provider*/    protected voidwrite () {//get the Contentresolver object using the Getcontentresolver () methodContentresolver resolver =Getcontentresolver (); Contentvalues Values=NULL; Values=Newcontentvalues (); Values.put ("Name", "Java"); //call the Insert method of the Contentresolver object to insert the dataResolver.insert (URI, values); Values=Newcontentvalues (); Values.put ("Name", "Swift");        Resolver.insert (URI, values); Values=Newcontentvalues (); Values.put ("Name", "Python");        Resolver.insert (URI, values); Values=Newcontentvalues (); Values.put ("Name", "C #");    Resolver.insert (URI, values); }    /*** Read the contents of the provider*/    protected voidRead () {StringBuilder Accum=NewStringBuilder (); //get the Contentresolver object using the Getcontentresolver () method//invokes the query method of the Contentresolver object, queries the data, and returns the cursor objectcursor cursor = getcontentresolver (). Query (URI,NULL,NULL,NULL,NULL);        Cursor.movetofirst ();  for(inti = 0, count = Cursor.getcount (); I < count; i++) {accum.append (cursor.getstring (Cursor.getcolumnindex ("Name")) . Append ("\ n");        Cursor.movetonext (); } System.err.println ("READ:" +Accum); }

Simple use of ContentProvider in Android

Related Article

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.