Android Development Basics Tutorial-Call Texting _android

Source: Internet
Author: User
Tags gettext
Call to send a text demo
Copy Code code 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 Action name
I.setaction ("Android.intent.action.CALL");
Set Intent Data
I.setdata (Uri.parse ("Tel:" + Phoneno));
Start activity
StartActivity (i);
}
Texting
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 = Toast.maketext (Mainactivity.this, "Send Complete", toast.length_long);
Toast.setgravity (gravity.top| Gravity.right, 50, 100);
Toast.show ();
}
}
@Override
public boolean Oncreateoptionsmenu (Menu 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 code 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= "Call"/>
<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= "Texting"/>
<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= "Please 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= "Please enter the text message"
android:ems= "Ten"/>
</RelativeLayout>
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.