SMS Receive
In Android, when a mobile phone receives a text message, it sends a Android.provider.Telephony.SMS_RECEIVED broadcast that carries all the data associated with the text message. Each application can listen to him in the broadcast receiver.
Simple SMS Receiving Program:
protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Sender=(TextView) Findviewbyid (R.id.sender); Content=(TextView) Findviewbyid (r.id.content); //Set FilterIntentfilter =NewIntentfilter (); Intentfilter.addaction ("Android.provider.Telephony.SMS_RECEIVED"); //Get Messagereceiver InstanceMessagereceiver =NewMessagereceiver (); //Register a broadcastRegisterreceiver (Messagereceiver,intentfilter); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); Unregisterreceiver (Messagereceiver); } //receive SMS Broadcast receivers classMessagereceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {//use the PDU key to extract an array of SMS PDUs, each of which represents an SMS messageObject[] Smspdus = (object[]) Intent.getextras (). Get ("PDUs"); //used to store messagessmsmessage[] Smsmessage =NewSmsmessage[smspdus.length]; /*** SMSMESSAGE.CREATEFROMPDU ((byte[]) smspdus[i], format); * The second parameter should be the type of SMS, GSM and CDMA SMS decoding method is different, so should pass this parameter **/String Format= Intent.getstringextra ("format"); //converts each PDU format array to a Smsmessage object for(inti = 0; i < smsmessage.length; i++){ if(Build.VERSION.SDK_INT < 23) {Smsmessage[i]= SMSMESSAGE.CREATEFROMPDU ((byte[]) smspdus[i]); } Else{Smsmessage[i]= SMSMESSAGE.CREATEFROMPDU ((byte[]) smspdus[i], format); } } //get the number sentString address = Smsmessage[0].getoriginatingaddress (); //link multiple pieces of content togetherStringBuilder messagecontent =NewStringBuilder (); for(Smsmessage message:smsmessage) {messagecontent.append (Message.getmessagebody ()); } //Set the view contentsSender.settext (address); Content.settext (Messagecontent.tostring ()); } }
Add in Androidmanifest.xml
<android:name= "Android.permission.RECEIVE_SMS"/>
Since the message system will send a broadcast, we should listen to this broadcast, to obtain the content of text messages. So start with a new broadcast receiver class Messagereceiver inherit from broadcast. Override the Onreceiver () method.
Main steps :
1.intent.getextras () method gets a Bundle object, Bundle.get ("PDUs"); Get SMS content; PDUs is an array of type object, and each object is a byte[] byte array, each of which is a text message.
2. Encapsulate the byte array into the Smsmessage type, after Android API 23 Smsmessage.createfrompdu ((byte[])); Not recommended, instead of SMSMESSAGE.CREATEFROMPDU ((byte[]), format); The second parameter represents the cell phone format; you can use Intent.getstringextra ("format"); Get.
The Getmessagebody () method of the 3.SmsMessage object can be used to obtain SMS content. Getoriginatingaddress () can get the sender's number.
So once the SMS is received, the system sends the Android.provider.Telephony.SMS_RECEIVED broadcast, it is received by the dynamically registered receiver, and then the Messagereceiver's Onreceiver () method is called.
SMS Truncation
Abortbroadcast () cannot truncate SMS after Android 4.4.
Send SMS
The Smsmanager object has the ability to send SMS messages, which can be obtained by calling the Smsmanager.getdefault () method.
The Smsmanager.sendtextmessage () method has 5 parameters, the first parameter is the target number, the third parameter is the SMS content, and the fourth parameter is a Pendingintent object.
pendingintent pi = pendingintent.getbroadcast (mainactivity.this,0,intent,0); The method can get a Pendingintent object.
Now let's look at an example:
Smsmanager Smsmanager =new Intent (); Sendintent.setaction ("sent_sms_action"= Pendingintent.getbroadcast (mainactivity. this, 0,sendintent,0); // Send SMS
/* The Sendtextmessage method has 5 parameters, the first parameter refers to the number of the specified recipient, and the third specified content */smsmanager.sendtextmessage ( To.gettext (). ToString (),null, Msginput.gettext (). ToString (), pi,null);
This means that when we send a text message, we write a broadcast of our own, send out a broadcast for sent_sms_action, because we operate on the virtual machine, we can listen to this broadcast to see if the text message is sent successfully.
Here to learn the basic SMS receiving and sending functions.
Learn to receive and send SMS on Android