Send SMS applets

Source: Internet
Author: User

The code structure.

Now let's look at the specific code.

Send. Java

Package cn.com. SMS. send; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. iterator; </P> <p> Import android. app. activity; <br/> Import android. app. pendingintent; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. telephony. smsmanager; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. edit Text; <br/> Import android. widget. toast; </P> <p> public class send extends activity {<br/> private string message; <br/> private string number; <br/> private edittext; <br/> private edittext edittext2; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> edittext = (edittext) This. findviewby ID (R. id. number); <br/> edittext2 = (edittext) This. findviewbyid (R. id. message); </P> <p> button = (button) This. findviewbyid (R. id. button); <br/> button. setonclicklistener (new view. onclicklistener () {</P> <p> Public void onclick (view v) {<br/> Number = edittext. gettext (). tostring (); <br/> message = edittext2.gettext (). tostring (); <br/> // you can view the number and message information in logcat. <br/> log. I ("Number", numb Er); <br/> log. I ("message", message); <br/>/* obtain the default information manager of the system. Note that smsmanager is Android. telephony. smsmanager; this is related to <br/> * the version we used. Before Android 2.0, we should use android. telephony. GSM. smsmanager <br/> * Android 2.0 and later versions should be used. telephony. smsmanager. <Br/> */<br/> smsmanager = smsmanager. getdefault (); <br/>/* pendingintent. getbroadcast returns a pendingintent object for broadcasting, similar to calling content. sendbroadcast (); <br/> */<br/> pendingintent paintent = pendingintent. getbroadcast (send. this, 0, new intent ("sms_sent"), 0); <br/> pendingintent deliveryintent = pendingintent. getbroadcast (send. this, 0, new intent ("sms_delivered"), 0); <br/> // smsmanager. dividemescript E. If the number of text messages exceeds the limit, we need this method to split the text message content. <Br/> arraylist <string> smses = smsmanager. dividemessage (Message); <br/> iterator <string> iterator = smses. iterator (); <br/> while (iterator. hasnext () {<br/> string temp = iterator. next (); <br/> // send an SMS <br/> smsmanager. sendtextmessage (number, null, temp, paintent, deliveryintent); <br/>}< br/> // The message content is displayed in a floating box, toast. length_long indicates the length of time displayed in the floating box. <br/> toast. maketext (send. this, "message sent successfully", toast. length_long ). show (); </P> <p >}< br/>}); </P> <p >}< br/>}

 

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "Welcome to text message sender, enter the phone number "<br/> <edittext <br/> Android: Id =" @ + ID/number "<br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: hint = "Enter the phone number here" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "Welcome to text message sender, enter the text message content "<br/> <edittext <br/> Android: Id =" @ + ID/message "<br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: minlines = "3" <br/> Android: hint = "Enter the text message here" <br/> <button <br/> Android: id = "@ + ID/button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "send" <br/> </linearlayout> <br/>

Androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "cn.com. SMS. send "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "8"/> <br/> <uses-Permission Android: Name = "android. permission. send_sms "> </uses-Permission> </P> <p> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". send "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> </P> <p> </Application> <br/> </manifest>

Eventually:

Like calling a applet, you also need to enable two avds to test the function.

Bytes ----------------------------------------------------------------------------------------

Waste of words:

The main class of the text messaging application is smsmanager. Android. telephony. GSM. smsmanager should be used before Android 2.0

Android. telephony. smsmanager should be used later;

Smsmanager = smsmanager. getdefault (); gets the default information manager of the system.

Bytes ---------------------------------------------------------------------------------------

Smsmanager. sendtextmessage (destinationaddress, scaddress, text, sentintent, deliveryintent)

-- Destinationaddress: the destination phone number.
-- Scaddress: the service provider's SMS center number (for example, China Mobile's SMS center number). This parameter can be left blank for testing.
-- Text: Text message content
-- Sentintent: Send --> China Mobile failed to send --> return the sending success or failure signal --> after processing, this intent encapsulates the message sending status.

-- Deliveryintent: Send --> China Mobile sends the message successfully --> Returns whether the recipient receives the message --> subsequent processing: this intent encapsulates the status information of the message received by the other party (the supplier has successfully sent the message but the other party has not received it ).

Bytes --------------------------------------------------------------------------------------

Public static pendingintent getbroadcast (context, int requestcode, intent, int flags)
Returns a pendingintent for broadcast, similar to calling the context. sendbroadcast () function.
Requestcode is not used now
Intent is the intent used for broadcast.
Flag includes flag_one_shot, flag_no_create, flag_cancel_current, and flag_update_current, which are used to set the attributes of the newly created pendingintent once. If not, the current attribute is not created, canceled, or updated.

------------------------------------------------------------------------------

In addition, we need to declare the SMS sending permission in androidmanifest. xml.

<Uses-Permission Android: Name = "android. Permission. send_sms"/>

-------------------------------------------------------------------------------

Sometimes, when we simulate two avds to send text messages, we will find that sometimes this program cannot be used normally. The system will prompt no dns servers found and the DNS service cannot be found. This is generally because your computer is not connected to the network.

 

 

  1. Send SMS:
  2. Smsmanager smsmgr = smsmanager. getdefault ();
  3. Smsmgr. sendtextmessage (address,Null, Message,Null,Null);
  4. The text message writing interface is displayed:
  5. Uri smstouri = URI. parse ("smsto: // 10086 ");
  6. Intent mintent =NewIntent (Android. content. Intent. action_sendto, smstouri );
  7. Startactivity (mintent );
  8. Send Email:
  9. Intent I =NewIntent (intent. action_send );
  10. I. putextra (intent. extra_email, address );
  11. I. putextra (intent. extra_subject, filename );
  12. I. putextra (intent. extra_stream, Uri. parse ("file: //" + filename ));;
  13. I. settype ("text/CSV ");
  14. Startactivity (intent. createchooser (I, "email file "));

 

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.