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>