Android Instance-Send Message

Source: Internet
Author: User

(1) Project structure:


Package Com.example.messagesender;import Java.util.arraylist;import Android.os.bundle;import android.app.Activity; Import Android.support.v4.widget.simplecursoradapter.viewbinder;import Android.telephony.gsm.smsmanager;import Android.view.menu;import Android.view.view;import Android.widget.button;import Android.widget.EditText;import Android.widget.toast;public class Mainactivity extends Activity {private EditText numbertext;//Mobile number Private EditText cont enttext;//Content Private button button1;//send button @overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); numbertext = (EditText) This.findviewbyid ( R.ID.EDITTEXT1) ContentText = (EditText) This.findviewbyid (r.id.edittext2); button1 = (Button) This.findviewbyid ( R.id.button1); Button1.setonclicklistener (new Buttonclicklistener ());} Private Final class Buttonclicklistener implements View.onclicklistener {@Overridepublic void OnClick (View v) {String num ber = Numbertext.getteXT (). toString (); String content = Contenttext.gettext (). toString ();//Get SMS Send Management Object Smsmanager Manager = Smsmanager.getdefault ();// For words over the time, split sms arraylist<string> texts = manager.dividemessage (content);//Iteration Send SMS for (String text:texts) { Manager.sendtextmessage (number, NULL, text, NULL, NULL);} Toast.maketext (Mainactivity.this, "sent successfully! ", Toast.length_short). Show ();}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

Activity_main.xml

<relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true "Android:text=" Please enter the mobile number "/> <edittext android:id=" @+id/edittext1 "android:layout_width=" Wrap_ Content "android:layout_height=" wrap_content "android:layout_alignleft=" @+id/textview1 "android:layou t_below= "@+id/textview1" Android:layout_margintop= "20DP" android:ems= "/> <textview android:id=" @+id/textview2 "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:layout_alignleft=" @+id/edittext1 "Android:        layout_below= "@+id/edittext1" android:layout_margintop= "18DP" android:text= "Please enter SMS Content"/> <edittext        Android:id= "@+id/edittext2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/textview2" android:layout_below= "@+id/textview2" Android:layout_margint op= "25DP" android:ems= "android:minlines=" 3 "> <requestfocus/> </EditText> & Lt Button android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "android:layout_below=" @+id/edittext2 "android:layout_margintop=" 18DP "android:text=" Send Message "/>& Lt;/relativelayout> 


Android Instance-Send Message

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.