Android sending information Simulation System

Source: Internet
Author: User

The Android system already has the familiar and commonly used sending information function, which is called the activity in the program, sometimes we need to call this application when writing an android program to send information through our program. The specific implementation method is as follows:

1. Create an XML layout file. The final effect is as follows:

    

2. Compile the code implementation function in the activity.

Import Java. util. arraylist; import android. OS. bundle; import android. widget. *; import android. app. activity; import android. telephony. smsmanager; import android. view. menu; import android. view. view;/** @ Author: fanchangfa */public class mainactivity extends activity {private edittext number; private edittext content; private button send; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main);/** initialization work: * text box and button **/number = (edittext) this.findviewbyid(r.id.txt _ number); content = (edittext) this.findviewbyid(r.id.txt _ content ); send = (button) This. findviewbyid (R. id. btn_send); send. setonclicklistener (New sendlistener ();} private final class sendlistener implements view. onclicklistener {public void onclick (view v) {// obtain the phone number and text message content string sms_number = number. gettext (). tostring (); string sms_content = content. gettext (). tostring (); // gets the default sending information instance smsmanager sms_manager = smsmanager. getdefault ();/* by default, one text message can send 70 Chinese characters. Therefore, the dividemessage method is called to split the content. ***/arraylist <string> txts = sms_manager.dividemessage (sms_content ); // after the text message is split, execute this loop to send messages in batches for (string TXT: txts) {/** first parameter: mobile phone number to be sent * second parameter: SMS center parameter (null: the system's default center address) * third parameter: SMS content * Fourth parameter: receipt status (whether the message is sent successfully) * fifth parameter: you can obtain whether the recipient has received the information status. ** the fourth and fifth parameters are obtained through the electrical signal returned by the mobile network. * asynchronous processing is supported. * only the broadcast receiver **/sms_manager.sendtextmessage (sms_number, null, txt, null, null );}
// You cannot determine whether the message is sent successfully. You can use the broadcast receiver and further input to determine whether the message is sent successfully. Toast. maketext (mainactivity. this, "sent successfully", toast. length_short) ;}}@ override public Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}

 

3. Just like the previous phone call, you must grant the permission to send information to perform this function. In the androidmainfest. xml file, add the following permissions:

  

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

4. Deploy the application to the simulator and enter the phone number and information to achieve the desired effect.

  

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.