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>