The Android phone we use will issue a system broadcast when it receives a text message. The broadcast contains detailed information about the text messages received. This article describes in detail how to listen for text messages by dynamically registering a broadcast.
There are two ways of registering a broadcast, one is dynamic registration and the other is static registration. Dynamic registration, as the name implies when the program is registered, need to use the broadcast when the registration, the end is destroyed. The static note is registered in the Androidmanifest.xml and is registered with the <receiver> tag in <application>.
So how to create a listen to the message of the broadcast receiver, in fact, only need a new class, let this class inherit Broadcastreceiver, and rewrite the parent class Onreceiver method can be. When a broadcast comes, the Onreceiver () method executes.
First create a Listenersmsbroadcast project. Then modify the Add code in mainactivity. Now create a Smsbroadcastreceiver class in mainactivity and inherit the Broadcastreceiver, and then override the Onreceiver method.
1 classSmsbroadcastreceiverextendsbroadcastreceiver{2 3 @Override4 Public voidonreceive (Context arg0, Intent Intent) {5
6Object[] object= (object[]) Intent.getextras (). Get ("PDUs");7StringBuilder sb=NewStringBuilder ();8 for(Object pdus:object) {9 byte[] pdusmsg= (byte[]) PDUs;TenSmsmessage sms=SMSMESSAGE.CREATEFROMPDU (pdusmsg); OneString mobile=sms.getoriginatingaddress ();//mobile phone number to send SMS AString Content=sms.getmessagebody ();//SMS Content - //here is the time to get the text message sent -Date date=NewDate (Sms.gettimestampmillis ()); theString date_time=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (date); - //Append to StringBuilder -Sb.append ("SMS Send number:" +mobile+ "\ nthe message content:" +content+ "\ n Send time:" +date_time+ "\ n"); -
+ } -Message msg=NewMessage (); +msg.what=receivered_msg; Amsg.obj=sb.tostring (); at handler.sendmessage (msg); - } - -}
As you can see from the code above, the Smsbroadcastreceiver class inherits the Broadcastreceiver and overrides the OnReceive () method. In the Onreceiver () method, use Intent.getextras (). Get ("PDUs") gets an array of type object[], and each object is of type byte. Then use the For loop to iterate through the array, and finally use handler to display the message content in the activity.
The following is a dynamic registration broadcast.
PrivateIntentfilter Intentfilter; PrivateSmsbroadcastreceiver Smsbroadcastreceiver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tv_msg=(TextView) Findviewbyid (r.id.tv_msg); Intentfilter=NewIntentfilter (); Intentfilter.addaction ("Android.provider.Telephony.SMS_RECEIVED"); Smsbroadcastreceiver=NewSmsbroadcastreceiver (); //Dynamic registration of broadcastsregisterreceiver (Smsbroadcastreceiver, Intentfilter); }
A Intentfilter object is declared in this code, and an action with a value of Android.provider.Telephony.SMS_RECEIVED is added to it. Why do you want to add this value to it? Because when the phone receives a text message, the system emits a broadcast with a value of Android.provider.Telephony.SMS_RECEIVED, which is the broadcast to listen to. A Smsbroadcastreceiver instance is then created. Then call the Registerreceiver () method to register the broadcast, where Smsbroadcastreceiver instances and Intentfilter instances are passed in, This way the Smsbroadcastreceiver will receive a broadcast when the system receives the SMS.
Note that this inside reads SMS, so need to add read SMS permission. Just add some permissions in the Androidmanifest.xml:
1 < android:name= "Android.permission.RECEIVE_SMS"></uses-permission >
Since the broadcast receiver is registered dynamically, be sure to unregister it. Unregister requires calling Unregisterreceiver () to implement. This is unregistered in the OnDestroy () method.
1 protected void OnDestroy () {2 Super . OnDestroy (); 3 // Cancel Registration of a broadcast 4 Unregisterreceiver (smsbroadcastreceiver); 5 }
This article uses the handler to modify the properties of the TextView text in the page to be worthwhile. Message.what to determine if you want to modify the value of text, Message.obj is the modified value.
1 PrivateHandler handler=NewHandler () {2 3 Public voidhandlemessage (android.os.Message msg) {4 if(msg.what==receivered_msg)5 {6Tv_msg.settext ("message received: \ n" +msg.obj);//\ n = line break7 }8 };9};
Android phones use radio to listen to messages received by mobile phones