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)