Android can send text messages and save them to the database, and receive and send reports.

Source: Internet
Author: User
        SmsManager smsMagager = SmsManager.getDefault();        // create the sentIntent parameter           Intent sentIntent = new Intent(SENT_SMS_ACTION);          PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent,                  0);          // create the deilverIntent parameter           Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);          PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0,                  deliverIntent, 0);                  String tel = editTel.getText().toString();        String msg = editMsg.getText().toString();        smsMagager.sendTextMessage( tel , null , msg , sentPI , deliverPI );

First, run the code. The above function enables android to send text messages to a specified number (for sentpi to get the report sent, deliverpi to get the report received by the other party). The difference is: send a report. The message is sent by the sender, and whether the recipient accepts the report or not. When the sender sends the text message to the recipient's mobile phone, the recipient's mobile phone will return a signal to the carrier and inform the carrier to receive the text message, the carrier then sends the signal to the sender. After the sender obtains the signal,

At the same time, you need to grant this application permission

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

To insert the text message you just sent to the database, add the following content:

Contentvalues values = new contentvalues (); // sending time values. put ("date", system. currenttimemillis (); // read status values. put ("read", 0); // 1 indicates receiving 2 indicates sending values. put ("type", 2); // delivered number values. put ("Address", TEL); // delivered content values. put ("body", MSG); // Insert the SMS library getcontentresolver (). insert (URI. parse ("content: // SMS/sent"), values );

Add the following Permissions

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

At first, I was worried that there would be a problem if I only inserted it into a table. Later I saw telephonyprovider and found that my idea was redundant because telephonyprovider would insert the data into each table.

Register two broadcasts ~ Receive and send reports ~

@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();registerReceiver(sendMessage, new IntentFilter(SENT_SMS_ACTION));registerReceiver(receiver, new IntentFilter(DELIVERED_SMS_ACTION));}

One is to send text messages, the other is to receive text messages,

Broadcastreceiver sendmessage = new broadcastreceiver () {@ overridepublic void onreceive (context c, intent) {// todo auto-generated method stub // determine whether the SMS is successfully sent. Switch (getresultcode () {Case activity. result_ OK: Toast. maketext (C, "sms sent successfully", toast. length_short ). show (); break; default: Toast. maketext (C, "failed to send", toast. length_long ). show (); break ;}}; broadcastreceiver receiver ER = new broadcastreceiver () {@ override public void onreceive (context, intent) {// indicates that the recipient has successfully received the SMS toast. maketext (context, "the recipient receives the message", toast. length_long ). show ();}};

Android SMS sending can be simulated in the simulator.

For example, if the ID of the first simulator is 5554,

Start the terminal
Telnet local host 5554
Enter help to see many functional commands used in simulators.
GSM call 134343434 // call the current simulator command
SMS send 15555218135 hello, this is a message // is a short message sent to the current Simulator

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: 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.