ContentProvider (i)

Source: Internet
Author: User

Registration ContentProvider:

< provider      Android:name = ". Provider. Userprovider "    android:authorities=" Com.rw.contentprovider.provider.UserProvider "     android:exported= "true"    ></ provider >

Where authorities is the domain name part of the URI and can be arbitrarily taken, but must be unique throughout the system

Name, like activity, tells the virtual machine to bind to the class

exported Specifies whether ContentProvider is allowed to be called by another application

The following 5 methods of inheriting ContentProvider:

According to their own business needs, to achieve the appropriate method

The following is the demo you wrote, with the support of Dbopenhelper class

 PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { Sqlitedatabase DataBase= This. Userdatabase.getreadabledatabase (); Switch(Usermatcher.match (URI)) { CaseUSER:LongRawid=Contenturis.parseid (URI); String where= "Uid=" +Rawid; if(selection!=NULL) {where+ = "+" +selection; }            returnDatabase.query ("Users", projection, where, Selectionargs,NULL,NULL, SortOrder);  CaseUSERS:returnDatabase.query ("Users", projection, selection, Selectionargs,NULL,NULL, SortOrder); default:            Throw NewIllegalArgumentException ("This unknown Uri:" +uri.tostring ()); }    }
 public   String getType (Uri uri) { switch   (Usermatcher.match (URI)) { case   USER:  return             "Vnd.android.cursor.dir/user" ;  case   USERS:  return " Vnd.android.cursor.item/user ";  default  :  throw  new  illegalargumentexception ("This is unknown Uri        : "+uri.tostring ()); }    }
 Publicuri insert (URI uri, contentvalues values) {sqlitedatabase dataBase= This. Userdatabase.getwritabledatabase (); Switch(Usermatcher.match (URI)) { CaseUSER:Throw NewIllegalArgumentException ("This Uri can not be insert a record to content!");  CaseUSERS:LongRawid=database.insert ("Users",NULL, values); returnContenturis.withappendedid (URI, Rawid); default:                        Throw NewIllegalArgumentException ("This unknown Uri:" +uri.tostring ()); }    }
 Public intDelete (Uri Uri, String selection, string[] selectionargs) {sqlitedatabase dataBase= This. Userdatabase.getwritabledatabase (); Switch(Usermatcher.match (URI)) { CaseUSER:LongRawid=Contenturis.parseid (URI); String where= "Uid=" +Rawid; if(selection!=NULL) {where+ = "+" +selection; }            returnDatabase.delete ("Users", where, Selectionargs);  CaseUSERS:returnDatabase.delete ("Users", Selection, Selectionargs); default:                        Throw NewIllegalArgumentException ("This unknown Uri:" +uri.tostring ()); }    }
 Public intupdate (URI Uri, contentvalues values, String selection, string[] selectionargs) {sqlitedatabase da Tabase=userdatabase.getwritabledatabase (); Switch(Usermatcher.match (URI)) { CaseUSER:LongRawid=Contenturis.parseid (URI); String where= "Uid=" +Rawid; if(selection!=NULL) {where+ = "+" +selection; }            returnDatabase.update ("Users", values, where, Selectionargs);  CaseUSERS:if(selection!=NULL){                returnDatabase.update ("Users", values, selection, Selectionargs); }Else{                Throw NewIllegalArgumentException ("This Uri con not update record from DataBase"); }        default:                        Throw NewIllegalArgumentException ("This Uri is unknown:" +uri.tostring ()); }    }

When the user uses Contentresolver to perform curd operations on the data, the corresponding method is called, in fact, the underlying is also based on the URI to obtain this contentprovider, The parameters are then passed to the corresponding method in the ContentProvider. Here are some of the code

 Public Final intupdate (URI Uri, contentvalues values, String where, string[] selectionargs) {//here, based on the URI passed in, gets to a contentproviderIcontentprovider Provider =Acquireprovider (URI); if(Provider = =NULL) {            Throw NewIllegalArgumentException ("Unknown URI" +URI); }        Try {            LongStartTime = Systemclock.uptimemillis ();//the Update method that really executes contentprovider here            introwsupdated =provider.update (Mpackagename, Uri, values, where, Selectionargs); LongDurationmillis = Systemclock.uptimemillis ()-StartTime; Maybelogupdatetoeventlog (Durationmillis, Uri,"Update", where); returnrowsupdated; } Catch(RemoteException e) {//arbitrary and not worth documenting, as Activity//Manager would kill this process shortly anyway.            return-1; } finally{Releaseprovider (provider); }    }

ContentProvider (i)

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.