PackageCom.example.message;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.telephony.SmsManager;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateEditText Et_number; PrivateEditText Et_message; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button Send=(Button) Findviewbyid (r.id.send); Et_number= (EditText) mainactivity. This. Findviewbyid (R.id.et_number); Et_message= (EditText) mainactivity. This. Findviewbyid (R.id.et_message); Send.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.send:sendmessage (); Break; } } Private voidSendMessage () {String number=Et_number.gettext (). toString (). Trim (); String message=Et_message.gettext (). toString (). Trim (); if(textutils.isempty (number) | |textutils.isempty (message)) {Toast.maketext ( This, "phone number or SMS content cannot be empty", Toast.length_short). Show (); return; }Else{Smsmanager Smsmanager=Smsmanager.getdefault (); Smsmanager.sendtextmessage (number,NULL, Message,NULL,NULL); } }}
Learning the first lesson, the second lesson is easy to imitate the first lesson, the only difficulty is
Smsmanager Smsmanager =smsmanager.getdefault (); null);
These two lines of code to achieve is the text message sent.
It is also important to note that the first lesson needs to add permissions, that is, the right to call is found in the Androidmanifest.xml permissions, find Call_phone
With the haircut SMS permission is send_sms