Android practice-SMS sender and android SMS sending

Source: Internet
Author: User

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

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.