ContentProvider contentresolver contentobserver content: Provide, access, monitor

Source: Internet
Author: User

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;


Urimatcher = new Urimatcher (urimatcher.no_match);

Urimatcher. Adduri (authority, "Person/insert", preson_insert_code);


public Boolean onCreate () {
mopenhelper = new Personsqliteopenhelper (GetContext ());
return true;

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);
throw new IllegalArgumentException ("uri mismatch:" + URI);
return null;



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 */


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

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: 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.