Android Learning 4-Implementation of SMS transmitter

Source: Internet
Author: User

Interface preview:

As can be seen from the figure, the app requires two TextView, one for displaying the mobile phone number, and the other for displaying the title of the SMS content.

Two edittext, one for storing mobile phone numbers and the other for storing incoming text messages.

A button to confirm the sending message.

So the code in Main_activity.xml is as follows:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "mobile number"/>

<edittext
Android:id= "@+id/main_activity_telephone"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>

<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "SMS Content"/>

<edittext
Android:id= "@+id/main_activity_message"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:minlines= "3"
android:maxlines= "7"/>

<button
Android:id= "@+id/main_activity_sendbutton"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Send"/>

</LinearLayout>

Code Explanation:

In this code, the layout is similar to the previous learning example, the difference is that the activity layout file has 5 controls, and the third control has more properties than the above 3 different places, that is minlines and MaxLines3, Minlines indicates that the minimum display height of this text input box is 3 rows of data, and Maxlines indicates the height of the text input box's maximum display height of 7 rows of data. The maximum row limit is added to prevent the button control from appearing too large when the text box becomes larger when the message is too large.

The code for the Mainactivity.java file is written:

Package com.example.messageSender;

Import java.util.ArrayList;

Import android.support.v7.app.ActionBarActivity;
Import Android.telephony.SmsManager;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;

public class Mainactivity extends Actionbaractivity {

Private Button Sendbutton;
Private EditText phonenumber,messages;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Sendbutton = (Button) Findviewbyid (R.id.main_activity_sendbutton);
PhoneNumber = (EditText) Findviewbyid (R.id.main_activity_telephone);
Messages = (EditText) Findviewbyid (r.id.main_activity_message);

Sendbutton.setonclicklistener (New Buttononclick ());
}

Private Final class Buttononclick implements view.onclicklistener{

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
String telephone = Phonenumber.gettext (). toString (). Trim ();
String sendmessages = Messages.gettext (). toString ();
Smsmanager Smsmanager = Smsmanager.getdefault ();
arraylist<string> Messagearr = Smsmanager.dividemessage (sendmessages);

for (String Message:messagearr) {
Smsmanager.sendtextmessage (telephone, NULL, message, NULL, NULL);
}

Toast.maketext (mainactivity.this, "message sent successfully", Toast.length_long). Show ();
}

}
}

In this code there are a lot of the same place as the previous routine, so I'll focus on the code in the OnClick method in the Buttononclick class (Secretly lazy O (∩_∩) o~)

In this code, the first is to get the phone number telephone and SMS content sendmessages, and then through the Smsmanager class to achieve the function of text messaging, through the Smsmanager.getdefault () method to get the SMS Send manager, Then through the SMS Send Manager's Dividemessage method, the content of the text message is divided into multiple SMS fragments (because the number of text messages sent by Android is limited, so for too long SMS content can be sent by this method to separate the message content into multiple messages), Finally call the SMS Send Manager Sendtextmessage method to send the text message out. For parameters in the Sendtextmessage method:

The first parameter is the phone number where the target is sent

The second parameter is the address of the service center and, if NULL, uses the current default SMS service center (if your phone is mobile, use the Mobile Service center, and if you are unicom, use the Unicom Service center ...). )

The third parameter is the content of the message being sent

The fourth parameter indicates how the sending succeeds and fails, if NULL, when the message is sent successfully or fails, the pendingintent is broadcast, the result code is ACTIVITY.RESULT_OK to indicate success, or result_error_ One of Generic_failure, Result_error_radio_off, RESULT_ERROR_NULL_PDU represents an error. For Result_error_generic_failure,sentintent may include an additional "error code" that contains a radio broadcast technology-specific value, usually only useful in repairing a failure. Here, I use null directly (refer to the official API for more detailed usage).

The fifth parameter is used to verify that the recipient has received the text change message. If it is not empty, the pendingintent is broadcast when the message is successfully delivered to the recipient.

Note: If your SMS content or receiver's mobile phone number is empty, it will produceIllegalArgumentException异常。

最后为了显示信息发送完毕,使用一个Toast弹出一个显示消息:

Toast.maketext (mainactivity.this, "message sent successfully", Toast.length_long). Show ();

The first parameter of Maketext represents a context object, the second parameter is represented as the displayed content, the last parameter is the length of time to display the Toast, Toast.length_long indicates a slightly longer display time, toast.length_ Short indicates a slightly shorter display time, and then don't forget to call the toast's Show method to display it.

 

Finally, as in the previous chapter of the Phone Dialer, because the app uses system permissions, so to add a user rights to the app, add the following code in the Androidmanifest.xml configuration:

<uses-permission android:name= "Android.permission.SEND_SMS"/>

Last run, the effect is as follows:

Android Learning 4-Implementation of SMS transmitter

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.