Android text message Monitoring (2) -- using ContentObserver to implement text message monitoring

Source: Internet
Author: User

MainActivity is as follows:

Package cc. testsmslistener; import cc. testsmslistener. SMSContentObserver. messageListener; import android.net. uri; import android. OS. bundle; import android. OS. handler; import android. widget. textView; import android. app. activity;/*** Demo Description: * use ContentObserver to implement SMS monitoring ** Note :*
 ** Reference: * http://blog.csdn.net/peijiangping1989/article/details/7368178 * Thank you very much **/public class MainActivity extends Activity {private TextView mTextView; // All messages public static final String URI = "content: // sms/"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mTextView = (TextView) findViewById (R. id. textView); // register the content observer SMSContentObserver smsContentObserver = new SMSContentObserver (new Handler (), this); this. getContentResolver (). registerContentObserver (Uri. parse (URI), true, smsContentObserver); // callback smsContentObserver. setOnReceivedMessageListener (new MessageListener () {@ Overridepublic void OnReceived (String message) {mTextView. setText (message );}});}}

SMSContentObserver:

Package cc. testsmslistener; import java. util. arrayList; import java. util. list; import android. app. activity; import android. database. contentObserver; import android. database. cursor; import android.net. uri; import android. OS. handler; public class SMSContentObserver extends ContentObserver {// all sms public static final String SMS_URI_ALL = "content: // sms/"; // inbox sms public static final String SMS_URI_INBOX = "content: // sms/inbox "; // public static final String SMS_URI_SEND =" content: // sms/sent "; // draft box text message public static final String SMS_URI_DRAFT =" content: // sms/draft "; private Activity mActivity; private List
 
  
MSmsInfoList; private MessageListener mMessageListener; public SMSContentObserver (Handler handler, Activity) {super (handler); this. mActivity = activity;} @ Overridepublic void onChange (boolean selfChange) {super. onChange (selfChange); Uri uri = Uri. parse (SMS_URI_INBOX); mSmsInfoList = this. getSmsInfo (uri, mActivity); mMessageListener. onReceived (mSmsInfoList. get (0 ). getSmsbody (); System. out. println ("Message content is:" + mSmsInfoList. get (0 ). getSmsbody (); System. out. println ("Message info is:" + mSmsInfoList. get (0);}/*** Note: * you only need to retrieve the first message in descending chronological order. * This is of course the latest message received */private List
  
   
GetSmsInfo (Uri uri, Activity activity) {List
   
    
SmsInfoList = new ArrayList
    
     
(); String [] projection = new String [] {"_ id", "address", "person", "body", "date", "type "}; cursor cusor = activity. managedQuery (uri, projection, null, null, "date desc limit 1"); int nameColumn = cusor. getColumnIndex ("person"); int phoneNumberColumn = cusor. getColumnIndex ("address"); int smsbodyColumn = cusor. getColumnIndex ("body"); int dateColumn = cusor. getColumnIndex ("date"); int typeColumn = cusor. g EtColumnIndex ("type"); if (cusor! = Null) {while (cusor. moveToNext () {SmsInfo smsinfo = new SmsInfo (); smsinfo. setName (cusor. getString (nameColumn); smsinfo. setDate (cusor. getString (dateColumn); smsinfo. setPhoneNumber (cusor. getString (phoneNumberColumn); smsinfo. setSmsbody (cusor. getString (smsbodyColumn); smsinfo. setType (cusor. getString (typeColumn); smsInfoList. add (smsinfo);} cusor. close ();} System. out. println ("smsInfoList. size () = "+ smsInfoList. size (); return smsInfoList;} // callback interface public interface MessageListener {public void OnReceived (String message);} public void setOnReceivedMessageListener (MessageListener messageListener) {this. mMessageListener = messageListener ;}}
    
   
  
 

SmsInfo is as follows:

Package cc. testsmslistener; public class SmsInfo {// SMS content private String smsbody; // the phone number that sent the SMS private String phoneNumber; // The message sending time private String date; // The name of the sender's private String name; // SMS type 1 is received, 2 is private String type; public String getSmsbody () {return smsbody ;} public void setSmsbody (String smsbody) {this. smsbody = smsbody;} public String getPhoneNumber () {return phoneNumber;} public void setPhoneNumber (String phoneNumber) {this. phoneNumber = phoneNumber;} public String getDate () {return date;} public void setDate (String date) {this. date = date;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getType () {return type;} public void setType (String type) {this. type = type ;}@ Overridepublic String toString () {return "SmsInfo [smsbody =" + smsbody + ", phoneNumber =" + phoneNumber + ", date =" + date + ", name = "+ name +", type = "+ type +"] ";}}

Main. xml is as follows:

     
  
 


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.