1. Based on the broadcast accept SMS 1.1 principle Android receives a text message and the system sends a Android.provider.Telephony.SMS_RECEIVED broadcast. Put it in bundle (Intent.extras), bundle can be understood as a map, text message using "PDUs" as the key, PDUs should be protocol description units shorthand, that is, a set of text messages. Android does not send a message to the broadcast immediately, he will have a certain delay, so there may be more than one message, so it will be used to store the array.
1.2 Implementation of the principle is clear, then the implementation is through the rewrite broadcastreceiver.onreceive (context context, Intent Intent) function, the key code is as follows:
Bundle bundle = Intent.getextras (); Object messages[] = (object[]) bundle.get ("PDUs"); Smsmessage smsmessage[] = new Smsmessage[messages.length];smsmessage[n] = SMSMESSAGE.CREATEFROMPDU ((byte[]) messages[ n]); sender = Smsmessage[n].getoriginatingaddress ();//Get message sender content = Smsmessage[n].getmessagebody ();// Get the content of a text message
The complete implementation is:
public class Eventreceiver extends Broadcastreceiver {private Context mcontext; private static final String TAG = "Rec_test"; @Override public void OnReceive (context context, Intent Intent) {Bundle bundle = Intent.getextras (); smsmessage[] smsmessages = null; object[] PDUs = null; if (bundle! = null) {PDUs = (object[]) bundle.get ("PDUs"); } if (PDUs!=null) {smsmessages = new smsmessage[pdus.length]; String sender = null; String content = null; for (int i=0; i<pdus.length; i++) {Smsmessages[i] = SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]); Sender = Smsmessages[i].getoriginatingaddress (); Content = Smsmessages[i].getmessagebody (); LOG.V (TAG, "SMS:" +sender+content); Showtoast (sender + "SMS", R.drawable.ic_dialog_email); Toast.maketext (Context, Sender + "," + content, Toast.length_long). Show (); }//for smsmessages}//if PDUs}}
1.3 Register Receiver
1.3.1 Static Registration
Define receiver in Androidmanifest.xml's application and set the action to receive.
Receiver needs to be placed in the application tag.
<receiver android:name= ". Eventreceiver "> <intent-filter> <action android:name=" android.provider.Telephony.SMS_ RECEIVED "/> </intent-filter> </receiver>
1.3.2 Dynamic Registration
function is called in the activity to register, and static content is similar. One parameter is receiver and the other is Intentfilter, which is the action to receive.
Private Broadcastreceiver receiver; @Override protected void OnStart () { super.onstart (); Receiver = new Callreceiver (); Registerreceiver (receiver, New Intentfilter ("Android.provider.Telephony.SMS_RECEIVED")); @Override protected void OnStop () { unregisterreceiver (receiver); Super.onstop ();
2. Based on Contentobserver monitoring SMS 2.1 principle
The principle is to listen to the SMS database, the operation of SMS content.
The following is an introduction to Contentobserver in the literature [3]:
"contentobserver--content Observer, the purpose is to observe (capture) the specific URI caused by the database changes, and then do some corresponding processing, it is similar to the database technology in the trigger (Trigger), When the URI observed by the contentobserver changes, it is triggered. Triggers are divided into table triggers, row triggers, and accordingly contentobserver are also classified as "table" Contentobserver, "Rows" contentobserver, of course, which is related to the URI MIME type it listens on. ”
Not to delve deeper into the bottom of the content, from the surface we can know that Contentobserver can obtain the URI caused by the database changes, the text message URI is:
Send SMS: Content://sms/outbox
Receive SMS: Content://sms/inbox
Knowing the URI, we can get the content of the text message.
2.2 Implementation
The method of implementation is to overload the Contentobserver.onchange () method to
The specific code is as follows:
Private class 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 the text message in the Sending box (the text message in the sending box is placed in the Outbox) cursor cursor = Getcontentresolver (). Query (Uri.parse ("Content://sms/outbox"), NULL, NULL, NULL, NULL), while (Cursor.movetonext ()) {StringBuilder sb = new StringBuilder (); Sb.append ("address=" + Cursor.getstring (Cursor.getcolumnindex ("Address")), Sb.append (", body=" +cursor.getstring (Cursor.getcolumnindex ( "Body")); Sb.append (", date=" +cursor.getstring (Cursor.getcolumnindex ("date")); LOG.I ("Observer", sb.tostring ());} Cursor.close (); Super.onchange (Selfchange);}}
2.3 Register Listener
Getcontentresolver (). Registercontentobserver (Uri.parse ("Content://sms"), True, new Smsobserver (New Handler ()));
3. Set permissions to receive some action, you need to add the appropriate permissions to the Androidmanifest.xml.
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>
4. References
[1] broadcastreceiver Introduction http://www.cnblogs.com/alwaysyouare/archive/2011/11/17/2252397.html
[2] Android phone, SMS monitor Http://blog.163.com/[email protected]/blog/static/1032422412013112343930567/
[3] The use of content watchers in Android----Contentobserver class http://www.cnblogs.com/slider/archive/2012/02/14/2351702.html
Android Monitor SMS 2 ways: Broadcast and Contentobserver