Android SMS transmitter source code

Source: Internet
Author: User

Activity class:

Import java.util.List;
Import android.app.Activity;
Import android.app.PendingIntent;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import android.widget.*;

public class Smsactivity extends Activity {
Private EditText Phonetext;
Private EditText ContentText;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

phonetext= (EditText) Findviewbyid (R.id.phonetext);
contenttext= (EditText) Findviewbyid (R.id.contenttext);

Sendsms ();
}

public void Sendsms () {
Button button= (button) Findviewbyid (R.id.button);
Button.setonclicklistener (New Smsonclick ());
}

Private Final class Smsonclick implements onclicklistener{
@Override
public void OnClick (View v) {
String Phonenumber=phonetext.gettext (). toString ();
String Content=contenttext.gettext (). toString ();

if (phonenumber==null| | Phonenumber.length () <1) {
Toast.maketext (Smsactivity.this, R.string.empty, Toast.length_short). Show ();
}else{
Smsmanager Smsmanager = Smsmanager.getdefault ();
Pendingintent sentintent = Pendingintent.getbroadcast (smsactivity.this,0, New Intent (), 0);
if (Content.length () > 70) {//If the number of words is more than 70, you need to split into multiple SMS send
List<string> msgs = smsmanager.dividemessage (content);
for (String msg:msgs) {
Smsmanager.sendtextmessage (PhoneNumber, NULL, MSG, sentintent, NULL);
The last two parameters are the broadcast intent that the SMS has sent, and the last parameter is the broadcast intent of the SMS message that the caller has received
}
} else {
Smsmanager.sendtextmessage (PhoneNumber, NULL, content, sentintent, null);
}
}
}
}
}

Manifest adding SMS Permission

<uses-permission android:name= "Android.permission.SEND_SMS"/>

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.