Run: Pre-Knowledge:
To listen for changes to the data of the specified ContentProvider, the Cotentobserver listener needs to be registered with Contentresolver to the specified URI. Contentresolver provides the following methods to register listeners:
publicfinal void registercontentobserver(Uriuri, Boolean notifyfordescendents, Contentobserver observer)
Parameter: URI: The URI of the contentprovider that the listener listens on.
Notifyfordescendents: False to indicate an exact match, that is, match only the URI, and true to indicate that it can match its derived URI at the same time.
Observer:contentobserver a derived listener instance.
Unregister listener:
Public finalvoid Unregistercontentobserver(Contentobserver observer)
Function: Cancels the observation of a given URI
Arguments: Observer Contentobserver instances of the derived class.
contentobserver--content Observer, the purpose is to observe (capture) a particular URI caused by the changes in the database, and then do some corresponding processing, it is similar to the database technology triggers (Trigger), when the Contentobserver observed by the URI changes , it will be triggered. Triggers are divided into table triggers, row triggers, and accordingly contentobserver are also divided into "table" Contentobserver, "Rows" contentobserver, which is, of course, the URI MIME type with which it listens
It's closed.
Contentobserver class Introduction:
The change that receives the callback. Must be implemented by being added to a Contentobservable object.
Construction Method:
|
Public constructors |
|
Contentobserver (Handler Handler)
OnChange () would happen on the provider Handler. |
Description: All derived classes of contentobserver need to call the constructor method
Parameters: Handler Handler object. It can be the main thread handler (which can update the UI at this time), or it can be any handler object.
Common methods:
|
Public Methods |
|
Boolean |
deliverselfnotifications ()
Returns TRUE if this observer are interested in notifications for changes made through the cursor The Observer is Registere D with. |
|
Final void |
Dispatchchange (Boolean Selfchange) |
|
void |
OnChange (Boolean Selfchange)
This method was called when a change occurs to the cursor, which is being observed. |
Description
void OnChange (Booleanselfchange)
Function: When the observed URI changes, callback the method to process. All derived classes of contentobserver need to overload the method to handle the logic.
Parameters: Selfchange callback, the value is generally false, the parameter is not very meaningful (I do not understand, the most important way to understand).
The steps to observe a particular URI are as follows:
1. To create our specific Contentobserver derived class, you must overload the parent class construction method, and you must overload the OnChange () method to handle the function implementation after the callback
2, use Context.getcontentresolover () to obtain the Contentresolove object, and then call the Registercontentobserver () method to register the content observer
such as:// Change the registration Listener Getcontentresolver () for content://sms data. Registercontentobserver (Uri. Parse
("Content://sms"), true,new smsobserver (new Handler ()));
3, because the life cycle of contentobserver is different from activity and service, so it is necessary to manually call Unregistercontentobserver () to cancel the registration when it is not needed.
SMS-related permissions:<!--send a message--
<uses-permission android:name= "Android.permission.SEND_SMS"/>
<!--reading messages--
<uses-permission android:name= "Android.permission.READ_SMS"/>
<!--writing messages--
<uses-permission android:name= "Android.permission.WRITE_SMS"/>
<!--receive messages--
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>
related Protocols : Content://sms/inbox Inbox
Content://sms/senthas been sent
Content://sms/draft Draft
Content://sms/outboxOutgoing Outbox
Content://sms/failed Send failed
content://sms/queuedList to send
SMS-related fields and descriptions in the database:
|
Field |
Description |
|
_id |
SMS number, such as 100 |
|
thread_id |
The serial number of the conversation, such as 100, with the same mobile phone number of each other SMS, its serial number is the same |
|
Address |
The sender's address, which is the mobile number, such as +86138138000 |
|
Person |
sender, if the sender is a specific name in the Address book, The Stranger is null |
|
Date |
Date, long type, such as 1346988516, can be set for date display format |
|
Protocol |
Protocol 0sms_rpoto SMS, 1mms_proto MMS |
|
Read |
Read 0 unread, 1 read |
|
Status |
SMS Status-1 receive, 0complete,64pending,128failed |
|
Type |
SMS Type 1 is received, 2 is issued |
|
Body |
Text Message specific content |
|
Service_center |
SMS Service center number, e.g. +8613800755500 |
Application Examples:
Package Com.jph.monitorsms;import Java.text.simpledateformat;import Java.util.date;import android.net.Uri;import Android.os.bundle;import Android.os.handler;import Android.widget.textview;import Android.widget.Toast;import Android.app.activity;import Android.database.contentobserver;import android.database.cursor;/** * Describe:</br > * Get the text message that the user is sending * This example gets the message that the phone is sending by changing the registration listener for CONTENT://SMS data. * @author JPH * date:2014.07.20 * */public class Monitorsms extends Activity {TextView txtview; @Overrideprotected void OnC Reate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); txtView= ( TextView) Findviewbyid (R.id.txtview);//Change the Registration Listener Getcontentresolver () for content://sms data. Registercontentobserver ( Uri.parse ("Content://sms"), True, new Smsobserver (New Handler ())); A listener class that inherits from Contentobserver classes Smsobserver extends Contentobserver{public smsobserver (Handler Handler) {super ( handler);//TODO auto-generated constructor stub} @Overridepublic void OnChange (bOolean selfchange) {//TODO auto-generated method stub//query sends a message to the box cursor cursor=getcontentresolver (). Query (Uri.parse ( "Content://sms/outbox"), NULL, NULL, NULL, or null);//Traverse query results get the text message while (Cursor.movetonext ()) {StringBuffer sb=new the user is sending StringBuffer ();//Get the sending address of the SMS Sb.append ("Send address:" +cursor.getstring ("Cursor.getcolumnindex ("));// Get the text message title Sb.append ("\ n title:" +cursor.getstring (Cursor.getcolumnindex ("subject")));//Get the content of the SMS sb.append ("\ n content:" + Cursor.getstring (Cursor.getcolumnindex ("Body")));//Gets the sending time of the SMS date date=new date (Cursor.getlong ( Cursor.getcolumnindex ("date")));//Format the date in seconds SimpleDateFormat sdf=new SimpleDateFormat ("yyyy years mm DD Day hh when mm min ss seconds"); Sb.append ("\ n Time:" +sdf.format (date)); System.out.println ("The text that is being sent to the query:" +sb.tostring ()); Toast.maketext (Monitorsms.this, sb.tostring (), Toast.length_long). Show (); Txtview.settext (sb.tostring ());} Super.onchange (Selfchange);}}}
Android Development Monitor sent SMS