Android enables texting/MMS features

Source: Internet
Author: User
Tags gettext xmlns

First Activity_main.xml layout:

<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"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Smsactivity ">

<textview
Android:id= "@+id/phone_lable"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparenttop= "true"
android:layout_margintop= "23DP"
android:text= "@string/phon_lable"/>


<edittext
Android:id= "@+id/edit_phone"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/phone_lable"
Android:layout_centerhorizontal= "true"
Android:ems= "10"

Android:inputtype= "Phone" >

</EditText>

<textview
Android:id= "@+id/sms_lable"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_alignleft= "@+id/edit_phone"
android:layout_below= "@+id/edit_phone"
android:layout_margintop= "22DP"
android:text= "@string/sms_lable"/>


<button
Android:id= "@+id/but_sms"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_alignleft= "@+id/edit_sms"
Android:layout_alignparentbottom= "true"
Android:layout_marginbottom= "14DP"
android:text= "@string/but_sms"/>


<edittext
Android:id= "@+id/edit_sms"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_above= "@+id/but_sms"
android:layout_alignleft= "@+id/sms_lable"
Android:ems= "10"
Android:inputtype= "Textmultiline"/>

</RelativeLayout>


Then add the permission to send SMS in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.SEND_SMS"/>

The next step is the implementation of the main code in smsactivity:

public class Smsactivity extends activity {


Declaring a Control object
Private Button butten_sms;
Private EditText Edit_phone;
Private EditText edit_sms;


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Set Display view
Setcontentview (r.layout.activity_sms);

Get button component
Butten_sms= (Button) Findviewbyid (r.id.but_sms);

Get the Input box component
Edit_phone = (edittext) Findviewbyid (R.id.edit_phone);

Edit_sms = (edittext) Findviewbyid (r.id.edit_sms);

Registering button Events
Butten_sms.setonclicklistener (New View.onclicklistener () {

@Override
public void OnClick (View arg0) {
/**
* Send MMS
*/
Get phone number
String phone_num = Edit_phone.gettext (). toString ();

Create Intent Object
Intent Intent = new Intent (intent.action_send);
Intent.putextra (Intent.extra_stream, Uri.parse ("file:///sdcard/a.jpg"));
Intent.putextra ("Address", phone_num);
Intent.putextra ("Exit_on_sent", true);
Intent.putextra ("Subject", "Subject:i Love You");
Intent.putextra ("Sms_body", "Content:: XXXX");
Intent.settype ("Image/jpeg");


StartActivity (Intent);

/*//Get SMS Manager Object
Smsmanager Smsmanager = Smsmanager.getdefault ();
Intent Object
Pendingintent pendingintent = Pendingintent.getbroadcast (
Smsactivity.this, 0, New Intent (), 0);

}
});
}


Texting function
public void Send () {
Get number
String phone_num = Edit_phone.gettext (). toString ();

Get the message sent
String phone_sms = Edit_sms.gettext (). toString ();

Getting an information manager object
Smsmanager Smsmanager = Smsmanager.getdefault ();

Intent Object
Pendingintent pendingintent = pendingintent.getbroadcast (smsactivity.this, 0, New Intent (), 0);

If the information length is greater than 70, it is sent in two articles. The default is 70
arraylist<string> contents = Smsmanager
. Dividemessage (Phone_sms);
for (String c:contents) {
Action to send information
Smsmanager.sendtextmessage (Phone_num, NULL, C,
Pendingintent, NULL);
}

/*//Send information operation
Smsmanager.sendtextmessage (Phone_num, NULL, phone_sms, pendingintent, NULL);

The effect of the toast
Toast.maketext (smsactivity.this, "SMS sent successfully", Toast.length_long). 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.sms, menu);
return true;
}

}

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.