Call and send text messages on the first day of Android Development

Source: Internet
Author: User

Call and send a text message demo

Public class mainactivity extends activity {edittext mphonenum, mmessage; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mphonenum = (edittext) findviewbyid (R. id. edittext1); mmessage = (edittext) findviewbyid (R. id. edittext2);} public void onclick (view v) {int id = v. GETID (); string phoneno = mphonenum. gettext (). tostring (); // call if (ID = R. id. button1) {intent I = new intent (); // set the action name I. setaction ("android. intent. action. call "); // set intent data I. setdata (URI. parse ("Tel:" + phoneno); // start the activity startactivity (I);} // send a text message to else if (ID = R. id. button2) {string smscontent = mmessage. gettext (). tostring (); // get the default SMS manager smsmanager Sm = smsmanager. getdefault (); SM. sendtextmessage (phoneno, null, smscontent, null, null); // Prompt user information, temporary notification Toast = toast. maketext (mainactivity. this, "sent successfully", toast. length_long); toast. setgravity (gravity. top | gravity. right, 50,100); toast. show () ;}@ override public Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}

Layout code:

<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" tools: context = ". mainactivity "> <button Android: Id =" @ + ID/button1 "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: layout_alignparentleft =" true "Android: layout_alignparenttop = "true" Android: onclick = "onclick" Android: text = ""/> <button Android: Id = "@ + ID/button2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparenttop = "true" Android: layout_torightof = "@ + ID/button1" Android: onclick = "onclick" Android: TEXT = "text"/> <edittext Android: Id = "@ + ID/edittext1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparentleft = "true" Android: layout_below = "@ + ID/button1" Android: EMS = "10" Android: hint = "Enter the phone number" Android: inputtype = "Number"> <requestfocus/> </edittext> <edittext Android: Id = "@ + ID/edittext2" Android: layout_width = "wrap_content" Android: layout_height = "100dip" Android: layout_alignparentleft = "true" Android: layout_below = "@ + ID/edittext1" Android: hint = "Enter the text message content" Android: EMS = "10"/> </relativelayout>

Add permissions to androidmainifest
<Uses-Permission Android: Name = "android. Permission. send_sms"/>
<Uses-Permission Android: Name = "android. Permission. call_phone"/>

Mobile development QQ group: 59516399
Http://download.csdn.net/detail/wenwei19861106/4975431 (csdn)

Related Article

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.