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)