Content offers
public class Personcontentprovider extends contentprovider{
private static final String authority = "Com.itheima28.sqlitedemo.providers.PersonContentProvider" ;
private static final int Preson_insert_code = 0; URI matching code for actions added by the person table
Urimatcher Urimatcher;
Private personsqliteopenhelper mopenhelper;
static{
Urimatcher = new Urimatcher (urimatcher.no_match);
Urimatcher. Adduri (authority, "Person/insert", preson_insert_code);
}
@Override
public Boolean onCreate () {
mopenhelper = new Personsqliteopenhelper (GetContext ());
return true;
}
@Override
Public URI insert (URI uri, contentvalues values) {
Switch (Urimatcher.match (URI)) {
Case Preson_insert_code://Add people to person table
Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
if (Db.isopen ()) {
Long id = db.insert ("person", null, values);
Db.close ();
Return Contenturis.withappendedid (URI, id);
}
Break
Default
throw new IllegalArgumentException ("uri mismatch:" + URI);
}
return null;
}
}
Urimatcher
Content access
contentresolver resolver = GetContext (). Getcontentresolver ();
URI = resolver. Insert (URI, values);
int count = Resolver. Delete (URI, where, Selectionargs);
int count = Resolver. Update (URI, values, "_id =?", New string[]{"20"});
cursor cursor = resolver. Query (URI, new string[]{"_id", "name", "age"}, NULL, NULL, "_id desc");
Content Monitoring
Resolver. Registercontentobserver (Uri.parse ("content://sms/"), True, new mycontentobserver(New Handler ())); Register for content Monitoring
Class Mycontentobserver extends contentobserver {
private static final String TAG = "Mycontentobserver";
Public Mycontentobserver (Handler Handler) {
Super (handler);
}
/** * Callback when the content being monitored is changed */
@Override
public void OnChange (Boolean selfchange) {
LOG.I (TAG, "SMS Changed");
Uri uri = uri.parse ("Content://sms/outbox"); The URI of the Outbox//Query the contents of the Outbox
cursor cursor = getcontentresolver (). Query (URI, new string[]{"Address", "date", "Body"}, NULL, NULL, NULL);
if (cursor! = NULL && cursor.getcount () > 0) {String address; Long date; String body;
while (Cursor.movetonext ()) {address = cursor.getstring (0); Date = Cursor.getlong (1); BODY = cursor.getstring (2);
LOG.I (TAG, "number:" + address + ", Date:" + Date + ", content:" + body); }
Cursor.close ();
} } }
ContentProvider contentresolver contentobserver content: Provide, access, monitor