SMS transmitter (version 1.0)

Source: Internet
Author: User
Tags sendmsg

This section learns the app development of SMS sending function, the effect

Front desk, Activity_main.xml 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:orientation= "Vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.swust.msg.MainActivity" > <!--input to phone type: 0~9 and #*---<EditText Android:id= "@+id/et_phone"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "@string/tishiphone"Android:inputtype= "Phone"/> <!--input is limited to five lines, the content in the input box is aligned at the top--<EditText Android:id= "@+id/et_msg"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Top"Android:hint= "@string/tishimsg"Android:lines= "5"/> <Button Android:id= "@+id/btnsend"Android:layout_width= "119DP"Android:layout_height= "64DP"Android:text= "Send"Android:onclick= "Sendmsg"/></linearlayout>

Backstage, Mainactivity.java code:

 Packagecom.swust.msg;Importjava.util.ArrayList;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.telephony.SmsManager;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidsendmsg (View v) {//get the number and content entered by the userEditText Et_phone =(EditText) Findviewbyid (R.id.et_phone); EditText et_msg=(EditText) Findviewbyid (r.id.et_msg); String Phone=Et_phone.gettext (). toString (); String msg=Et_msg.gettext (). toString (); //Send SMS//1. Get the short interest managerSmsmanager SM =Smsmanager.getdefault (); //2. Send SMS        /*The second parameter is the short service center, is empty can * the third parameter to send the broadcast, is empty can * the 4th parameter is to accept the broadcast, is empty can*/        //sm.sendtextmessage (phone, NULL, MSG, NULL, NULL); //2. Cut short messages and divide long text messages into small text messagesArraylist<string> smss=sm.dividemessage (msg); //3.for Loop Send all the text messages in the collection         for(String String:smss) {sm.sendtextmessage (phone,NULL, String,NULL,NULL); }    }}

Of course need to add permission, in the manifest file permission has the following permissions:

<uses-SDK Android:minsdkversion= "14"android:targetsdkversion= "/>"<uses-permission android:name= "Android.permission.SEND_SMS"/><Application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <Activity Android:name=". Mainactivity "Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter&        Gt </activity> </application>

Second Edition:

The core statement about sending text messages is explained here:

Http://www.cnblogs.com/nktblog/articles/2607086.html

 Packagecom.swust.msg;Importjava.util.ArrayList;Importandroid.app.Activity;Importandroid.app.PendingIntent;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.telephony.SmsManager;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidsendmsg (View v) {//get the number and content entered by the userEditText Et_phone =(EditText) Findviewbyid (R.id.et_phone); EditText et_msg=(EditText) Findviewbyid (r.id.et_msg); String Phone=Et_phone.gettext (). toString (); String msg=Et_msg.gettext (). toString (); //Send SMS//1. Get the short interest managerSmsmanager SM =Smsmanager.getdefault (); //2. Send SMS        /*The second parameter is the short service center, is empty can * the third parameter to send the broadcast, is empty can * the 4th parameter is to accept the broadcast, is empty can*/        //sm.sendtextmessage (phone, NULL, MSG, NULL, NULL); //2. Cut short messages and divide long text messages into small text messagesArraylist<string> smss=sm.dividemessage (msg); //3.for Loop Send all the text messages in the collection         for(String String:smss) {sm.sendtextmessage (phone,NULL, String,NULL,NULL); }        /*first of all, the phone length is 11 or 6 (test our school) and the content is not empty, send out the information; The disadvantage of returning a message after a successful or unsuccessful delivery is that the message (MSG) is sent directly by using the Cut SMS Loop function .*/        if((Phone.trim (). Length () ==11| | Phone.trim (). Length () ==6) &&msg.trim (). Length ()!=0) {pendingintent pintent=pendingintent.getbroadcast (mainactivity. This, 0,NewIntent (), 0); Sm.sendtextmessage (Phone,NULL, MSG, Pintent,NULL); Toast.maketext (mainactivity. This, "sent successfully", Toast.length_short). Show (); }Else{toast.maketext (mainactivity). This, "Send Failed", Toast.length_short). Show (); }      }}

SMS transmitter (version 1.0)

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.