Content providers Content Provider
If you want to access the shared data in ContentProvider, you must use the Contentresolver class for CRUD operations
Insert () Add Data Update () Updates data Delete () Remove data query () queries data
Getcontentresolver (). query (URI, projection, selection, Selectionargs, sortOrder);
Query () method parameters |
corresponding SQL section |
Describe |
Uri |
From table_name |
Specify to query a table under an application |
Projection |
Select Column1, Column2 |
Specify the column name of the query |
Selection |
where column = value |
Specify the WHERE constraint |
Selectionargs |
- |
Provide a specific value for a placeholder in the where |
By |
Order by Column1, Column2 |
Specify how query results are sorted |
-
In the method to write additions and deletions to the database code, for example, add method
@Override public uri insert (URI Uri, contentvalues Values) {Db.insert ( person, null , values); return Uri;}
Define the label of the content provider in the manifest file, note that you must have the authorities attribute , which is the host name of the content provider, which functions like an address
< Provider
android:name= "Com.example.contentprovider.PersonProvider" android:authorities = "Com.example.person" android:exported= "true">
</ provider>
Create a different app, access a custom content provider, implement an insert operation on the database
Public void Click (View v) { // Get the Content Decomposition object contentresolver cr = getcontentresolver (); New contentvalues(); CV.put("name", "Small square"); CV.put("Phone", 138856); CV.put("money", +); // URL: The host name of the content provider Cr.Insert(uri.parse ("Content://com.example.person"), CV);}
Urimatcher
- Used to determine which match between a URI and a specified number of URIs
Add a matching rule
// Specify multiple URIUm.adduri ("Com.example.person", "person",Person_code), Um.adduri ("Com.example.person", " Company ", Company_code); // #号可以代表任意数字Um.adduri ("Com.example.person", "person/#", Query_one_person_code);
Different tables can be implemented by URI-matching device
@Override PublicURI insert(URI uri, contentvalues values) {if(Um.match (uri) = =Person_code) {Db.insert ("Person",NULL, values); } Else if(Um.match (uri) = =Company_code) {Db.insert ("Company",NULL, values); } Else{ Throw Newillegalargumentexception (); } returnURI;}
If the path has numbers, the API that pulls the numbers out
ContentUris.parseId(uri);
SMS Database
- Only need to focus on SMS tables
- Just 4 fields to focus on
- Body: SMS Content
- Address: The sender or recipient number of the SMS (the buddy's number to chat with you)
- Date: SMS Time
- Type:1 for Receive, 2 for send
read the system text message , the first query source to obtain the text message database content provider hostname and path, and then
Contentresolver CR = getcontentresolver(); Cursor C= Cr.query ( Uri. Parse ("content://sms"),New string[]{"Body", "date", "Address", "type"},NULL,NULL,NULL); while(C.movetonext ()) {String body= c.getstring (0); String Date= c.getstring (1); String Address= C.getstring (2); String type= C.getstring (3); System.out.println (Body+ ";" + Date + ";" + address + ";" +type);}
Insert System SMS
Contentresolver cr =new contentvalues () cv.put ("Body", "you tail number to the Bank of XXXX debit card received a transfer of RMB 1,000,000" ); Cv.put ("Address", 95555), Cv.put ("type", 1); Cv.put ("Date", System.currenttimemillis ()); Cr.insert (Uri.parse ("content://sms"), CV);
- Insert query system SMS requires registration permission
Contact database
- raw_contacts Table :
- data Table : Contact details, one line
- DATA1: Specific content of information
- RAW_CONTACT_ID: Contact ID that describes which contact the information belongs to.
- MIMETYPE_ID: Describe what type of information it belongs to
- mimetypes table : View specific types by mimetype_id to this table
Reading contacts
new String[]{"contact_id"}, null, null, null);
Then take the contact ID and go to the data table and query the information that belongs to the contact .
Cursor c = cr.query(Uri.parse("content://com.android.contacts/data"),
new String[]{"data1", "mimetype"}, "raw_contact_id = ?", new String[]{contactId}, null);
-
Gets the value of the Data1 field, which is the contact's information, What kind of information is judged by mimetype
while (C.movetonext ()) {String Data1 = C.getstring (0 = c.getstring (1); if ("Vnd.android.cursor.item/email_v2" .equals (MimeType)) {Contact.setemail (data1); else if (" Vnd.android.cursor.item/name ".equals (MimeType)) {contact.setname (data1); else if (" Vnd.android.cursor.item/phone_v2 ".equals (MimeType)) {Contact.setphone (data1); }}
Insert Contact
- Query the Raw_contacts table first to determine how much the new contact ID should be
Insert the identified contact ID into the raw_contacts table
cv.put("contact_id", _id);cr.insert(Uri.parse("content://com.android.contacts/raw_contacts"), cv);
Inserting data in the database table
Insert 3 fields: Data1, MimeType, raw_contact_id
New contentvalues (), Cv.put ("Data1", "Zhao Liu"), Cv.put ("MimeType", "Vnd.android.cursor.item/name"); Cv.put ("raw_contact_id", _id); Cr.insert (Uri.parse ("Content://com.android.contacts/data" ) New contentvalues (); Cv.put ("Data1", "1596874"); Cv.put ("MimeType", "Vnd.android.cursor.item/phone _v2 "), Cv.put (" raw_contact_id ", _id); Cr.insert(Uri.parse (" content://com.android.contacts/ Data "), CV);
Content Viewer
When the database data changes, the content provider notifies you that a content watcher is registered on the content provider's URI to receive notification of changes to the data
Cr.Registercontentobserver(Uri.parse ("Content://sms"),true,NewMyobserver (NewHandler ()));classMyobserverextends contentobserver{ Publicmyobserver (Handler Handler) {Super(handler); //TODO auto-generated Constructor stub } //This method is called when the content watcher receives a notification that the database has changed@Override Public void OnChange(BooleanSelfchange) { }}
Code to send notifications to content providers
ContentResolver cr = getContext().getContentResolver();//发出通知,所有注册在这个uri上的内容观察者都可以收到通知cr.notifyChange(uri, null);
Content Provider--sharing data across programs