Basic Android development tutorial-send a text message by phone

Source: Internet
Author: User

Call and send a text message demo CopyCode The Code is as follows: 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
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: Copy code The Code is as follows: <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 = "phone"/>
<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 message"/>
<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 text message content"
Android: EMS = "10"/>
</Relativelayout>

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.