Mainactivity 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 achieve SMS monitoring * * Note permissions: * <uses-permission android:name= "Android.permission.READ_SMS"/> * * Test information: * http://blog.csdn.net/peijiangping1989/article/details/7368178 * Thank you very much * */public class mainactivity E Xtends Activity {Private TextView mtextview;//all SMS public static final String URI = "content://sms/"; @Overrideprotected vo ID onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); init ();} private void Init () {mtextview= (TextView) Findviewbyid (R.id.textview);//registered 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 as follows:
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"; Outbox SMS public static final String sms_uri_send = "content://sms/sent";//Draft box SMS public static final String sms_uri_draft = "C Ontent://sms/draft ";p rivate Activity mactivity;private list<smsinfo> msmsinfolist;private MessageListener Mmessagelistener;public Smscontentobserver (Handler Handler, activity 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: * This is only used to remove the first bar in descending order of time * This is of course the latest message */private list<smsinfo> getsmsinfo (Uri uri,activity Activity) {list< Smsinfo> smsinfolist=new arraylist<smsinfo> (); 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.getcolumninde X ("person"), int phonenumbercolumn = Cusor.getcolumnindex ("address"), int smsbodycolumn = Cusor.getcolumnindex ("Body") int datecolumn = Cusor.getcolumnindex ("date"), int typecolumn = Cusor.getcolumnindex ("type"), if (cusor! = null) {while (c Usor.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 as follows:
package Cc.testsmslistener;public class Smsinfo {//SMS content private string smsbody;//send SMS Phone number private string phonenumber;//send SMS Time P Rivate string date;//The name of the person who sent the message private string name;//SMS Type 1 is received, 2 is issued 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 as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <textview android:id= "@+id/textview" android:layout_width= "Wrap_content" android: layout_height= "Wrap_content" android:text= "@string/hello_world" android:layout_centerinparent= "true" /></relativelayout>