Android practice-SMS sender and android SMS sending
First, design the interface
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "android: orientation =" vertical "> <EditText android: layout_width =" match_parent "android: layout_height =" wrap_content "android: hint = "Enter the number"/> <EditText android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "Enter the content"/> </LinearLayout>
If you want to enlarge the input content box
Configurable
Android: lines = "6"
Then add the button.
Complete code:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "android: orientation =" vertical "> <EditText android: id =" @ + id/et_phone "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: hint = "Enter the number"/> <EditText android: id = "@ + id/et_content" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "Enter the content" android: lines = "6"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "send" android: text = "send"/> </LinearLayout>
The interface is as follows:
An error is reported after you click send. The permission is not set.
Create another simulator avd2.3 (low version and fast running)
Start two simulators at the same time
Note: The SMS sender number is 5556.
The above completes the simple text message sending function.
However, the above operation still has a small problem, that is, when the length of the text message exceeds the length limited by the carrier, the text message cannot be sent
The modification code is as follows:
Package com. wuyudong. smssender; import java. util. list; import android. OS. bundle; import android. app. activity; import android. telephony. smsManager; import android. view. menu; import android. view. view; import android. widget. editText; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void send (View v) {EditText et_phone = (EditText) findViewById (R. id. et_phone); EditText et_content = (EditText) findViewById (R. id. et_content); String phone = et_phone.getText (). toString (); String content = et_content.getText (). toString (); // get the SMS manager object SmsManager sm = SmsManager. getDefault (); // if the text message is too long, the text message cannot exceed the maximum number of characters specified by the carrier List <String> smss = sm. divideMessage (content); for (String sms: smss) {// send sms // arg0: Recipient's number // arg1: Number of the sms service center; do not set // arg2: SMS content sm. sendTextMessage (phone, null, sms, null, null );}}}
Done