Text message sent by Android listening

Source: Internet
Author: User

Text message sent by Android listening
Run: prerequisites:

To listen for changes to the data of the specified ContentProvider, you must register the CotentObserver listener with the specified Uri through ContentResolver. ContentResolver provides the following methods:Register the listener:

Publicfinal void registerContentObserver (Uriuri, boolean policyfordescendents, ContentObserver observer)

Parameter: uri: The Uri of the ContentProvider monitored by the listener.

Policyfordescendents: false indicates exact match, that is, only the Uri is matched. true indicates that the derived Uri can be matched at the same time.

Observer: Listener instance derived from ContentObserver.

Cancel listener registration:

Public finalvoid unregisterContentObserver (ContentObserver observer)

Function: Disable the observation of a specified Uri.

Parameter: an instance of the observer ContentObserver derived class.

ContentObserver-content observer, which is used to observe (capture) Changes in the database caused by a specific Uri and then perform corresponding processing. It is similar to a Trigger in database technology ), when the Uri observed by ContentObserver changes, it is triggered. Triggers are divided into Table triggers and row triggers. Correspondingly, ContentObserver is also divided into "table" ContentObserver "and" row "ContentObserver. Of course, this is consistent with the Uri MIME Type it listens.

.

ContentObserver class introduction:

Receives callback changes. It must be implemented by being added to a ContentObservable object.

Constructor:

Public Constructors

ContentObserver (Handler handler)

OnChange () will happen on the provider Handler.

Note: This ContentObserver construction method must be called for all ContentObserver Derived classes.

Parameter: handler Handler object. It can be the main thread Handler (you can update the UI at this time) or any Handler object.

Common Methods:

Public Methods

Boolean

DeliverSelfNotifications ()

Returns true if this observer is interested in configurations for changes made through the cursor the observer is registered.

Final void

DispatchChange (boolean selfChange)

Void

OnChange (boolean selfChange)

This method is called when a change occurs to the cursor that is being observed.

Note:

Void onChange (booleanselfChange)

Function: calls back this method when the observed Uri changes. All ContentObserver Derived classes must reload this method to process the logic.

Parameter: After selfChange callback, its value is generally false. this parameter is of little significance (I do not understand it, and it is the most important to understand the method ).

Follow these steps to observe a specific Uri:

1. To create a specific ContentObserver derived class, you must reload the parent class constructor and the onChange () method must be reloaded to implement the callback function.

2. Use context. getContentResolover () to obtain the ContentResolove object, and then call registerContentObserver () to register the content observer.

For example: // for content: // sms, the registration listener getContentResolver (). registerContentObserver (Uri.Parse

("Content: // sms "),True,NewSmsObserver (NewHandler ()));

3. Because the life cycle of ContentObserver is not synchronized with Activity and Service, you need to manually call unregisterContentObserver () to cancel registration when you do not need it.


SMS related permissions:






Related Protocols: Content: // sms/inbox Inbox
Content: // sms/sent Sent
Content: // sms/draft Draft
Content: // sms/outbox Sender
Content: // sms/failed Failed to send
Content: // sms/queued List to be sent

Sms-related fields and descriptions in the database:

Field

Description

_ Id

SMS number, such as 100

Thread_id

The conversation serial number, such as 100, is the same as the serial number of the text message sent by the same mobile phone number.

Address

Sender address, that is, the mobile phone 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, you can set the date display format

Protocol

Protocol 0SMS_RPOTO SMS, 1MMS_PROTO MMS

Read

Whether to read 0 unread, 1 read

Status

SMS status-1 received, 0 complete, 64 pending, 128 failed

Type

SMS type 1 is received, and 2 is sent

Body

SMS content

Service_center

Number of the SMS service center, for example, + 8613800755500

Application instance:
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:
* Get the text message that the user is sending * This instance obtains the message that the mobile phone is sending by changing the registration listener for the content: // sms data. * @ Author JPH * Date: 2014.07.20 **/public class MonitorSms extends Activity {TextView txtView; @ Overrideprotected void onCreate (Bundle savedInstanceState) Comment View); // content: // sms Data Change register listener getContentResolver (). registerContentObserver (Uri. parse ("content: // sms"), true, new SmsObserver (new Handler ()));} // a listener class SmsObserver extends ContentObserver {public SmsObserver (Handler handler) {super (handler) inherited from ContentObserver ); // TODO Auto-generated constructor stub} @ Overridepublic void onChange (boolean selfChange) {// TODO Auto-generated method stub // query the text message Cursor cursor = getContentResolver () sent to the box (). query (Uri. parse ("content: // sms/outbox"), null, null); // retrieve the text message while (cursor. moveToNext () {StringBuffer sb = new StringBuffer (); // obtain the SMS Sending address sb. append ("Sending address:" + cursor. getString (cursor. getColumnIndex ("address"); // obtain the title sb of the SMS. append ("\ n title:" + cursor. getString (cursor. getColumnIndex ("subject"); // obtain the sb. append ("\ n content:" + cursor. getString (cursor. getColumnIndex ("body"); // obtain the SMS sending time Date date = new Date (cursor. getLong (cursor. getColumnIndex ("date"); // format the date in seconds. SimpleDateFormat sdf = new SimpleDateFormat ("MM dd, yyyy, hh, mm, ss "); sb. append ("\ n time:" + sdf. format (date); System. out. println ("the queried text message is being sent:" + sb. toString (); Toast. makeText (MonitorSms. this, sb. toString (), Toast. LENGTH_LONG ). show (); txtView. setText (sb. toString ();} super. onChange (selfChange );}}}



Related Article

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: info-contact@alibabacloud.com 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.