Android send and receive SMS

Source: Internet
Author: User
Tags gettext

Effect: Click Send Text message to start sending SMS

Show the text message when you receive it

The code is as follows:

I. Statement of authority
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>  <uses-permission android:name= " Android.permission.SEND_SMS "/>  
Second, the Code implementation

Our SMS features are used in the two main classes of Smsmanager and Smsmessage.

When the SMS is not received, manually in the application management of the app's SMS send and receive permission to open

java/** * Created by Linda on 16/5/14.  * Send and receive SMS * Eliminate international standards the length of each text message does not exceed 160 characters * If the text message length is too long, you can call Sendmultiparttextmessage () to separate multiple text messages to send */public class Mesaageactivity      Extends Activity {private TextView sender;      Private TextView content;        Private EditText to;//receive SMS mobile phone number private EditText msginput;//send the contents of the message private Button send;        Private Intentfilter receivefilter;//SMS receive private messagereceiver Messagereceiver;        Private Intentfilter sendfilter;//SMS send private sendstatusreceiver sendstatusreceiver;          @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);          Setcontentview (R.layout.message);          Sender = (TextView) Findviewbyid (R.id.sender);          Content = (TextView) Findviewbyid (r.id.content);          to = (EditText) Findviewbyid (r.id.to);          Msginput = (EditText) Findviewbyid (r.id.msg_input);  Send = (Button) Findviewbyid (r.id.send); Sign up for SMSReceived broadcast Receivefilter = new Intentfilter ();          Receivefilter.addaction ("Android.provider.Telephony.SMS_RECEIVED");          Receivefilter.setpriority (100);          Messagereceiver = new Messagereceiver ();  Registerreceiver (Messagereceiver, Receivefilter);          Register monitor SMS send broadcast sendfilter = new Intentfilter ();          Sendfilter.addaction ("Sent_sms_action");          Sendstatusreceiver = new Sendstatusreceiver ();  Registerreceiver (Sendstatusreceiver, Sendfilter);  Click the button to send SMS Send.setonclicklistener (new View.onclicklistener () {@Override public                  void OnClick (View v) {Smsmanager Smsmanager = Smsmanager.getdefault ();                  Intent sentintent = new Intent ("Sent_sms_action");                  pendingintent pi = pendingintent.getbroadcast (mesaageactivity.this, 0, sentintent, 0); Send text messages based on number and content (requires permission) Smsmanager.sendtextmessage (tO.gettext (). ToString (), NULL, Msginput.gettext (). ToString (), pi, null);      }          });          } @Override protected void OnDestroy () {Super.ondestroy ();          Unregisterreceiver (Messagereceiver);      Unregisterreceiver (Sendstatusreceiver); }//SMS received Broadcast class Messagereceiver extends Broadcastreceiver {@Override public void onreceive (Co              ntext context, Intent Intent) {Bundle bundle = Intent.getextras ();              Object[] PDUs = (object[]) bundle.get ("PDUs");//Get SMS array smsmessage[] messages = new Smsmessage[pdus.length]; for (int i = 0; i < messages.length; i++) {Messages[i] = SMSMESSAGE.CREATEFROMPDU ((byte[ ] pdus[i]);//convert PDU character array to Smsmessage object} String address = Messages[0].getoriginatingaddress ();//Received              Take the SMS sender's number String fullmessage = "";          for (Smsmessage message:messages) {        Fullmessage + = Message.getmessagebody ();//Get SMS Content} sender.settext (address);              Content.settext (Fullmessage);          Abortbroadcast ();  }}//Listen whether the broadcast is sending a successful broadcast class Sendstatusreceiver extends Broadcastreceiver {@Override public void OnReceive (Context context, Intent Intent) {if (getresultcode () = = RESULT_OK) {Toast.              Maketext (Context, "Send succeeded", Toast.length_long). Show ();              } else {Toast.maketext (context, "Send failed", Toast.length_long). Show ();   }          }        }    }

  

Android send and receive SMS

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.