Android SMS receive monitor, automatic reply SMS operation example _android

Source: Internet
Author: User

To define an action for a broadcast receiver:

Copy Code code as follows:

private static final String TAG = "SMSService";
/**
* Information Send status Broadcast
*/
private static final String action_sms_send = "com. Smsservice.send ";
/**
* Information Receive status broadcast
*/
private static final String action_sms_delivery = "com. Smsservice.delivery ";
/**
* Information Receive Broadcast
*/
private static final String Action_sms_receiver = "Android.provider.Telephony.SMS_RECEIVED";

Define and register a broadcast receiver, either dynamically or statically, to register:

Copy Code code as follows:

Class Smsreceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
int resultcode = Getresultcode ();
if (Intent.getaction (). Equals (Action_sms_receiver)) {
LOG.I (TAG, "smsreceiver->onreceive");
Smsmessage SMS = NULL;
Bundle Bundle = Intent.getextras ()//get content in intent
if (bundle!= null) {
Object[] PDUs = (object[]) bundle.get ("PDUs");//Get the contents of the bundle
for (Object Obj:pdus) {
The following two lines are taken out and added to the message
SMS = SMSMESSAGE.CREATEFROMPDU ((byte[]) obj);
}
}
Mhandler.obtainmessage (msg_sms_receive,sms). Sendtotarget ();
Receive Message Send status
}else if (intent.getaction (). Equals (Action_sms_send)) {
Switch (ResultCode) {
Case ACTIVITY.RESULT_OK:
LOG.I (TAG, "SMS sent successfully");
Break
Case Smsmanager.result_error_generic_failure:
LOG.I (TAG, "SMS Send failure: generic_failure");
Break
Case Smsmanager.result_error_no_service:
LOG.I (TAG, "SMS Send failure: No_service");
Break
Case SMSMANAGER.RESULT_ERROR_NULL_PDU:
LOG.I (TAG, "SMS Send failure: NULL_PDU");
Break
Case Smsmanager.result_error_radio_off:
LOG.I (TAG, "SMS Send failure: Radio_off");
Break
}
Receive information receive status
}else if (intent.getaction (). Equals (Action_sms_delivery)) {
Switch (ResultCode) {
Case ACTIVITY.RESULT_OK:
LOG.I (TAG, "SMS received success");
Break
Case Smsmanager.result_error_generic_failure:
LOG.I (TAG, "message received failure: Generic_failure");
Break
Case Smsmanager.result_error_no_service:
LOG.I (TAG, "message received failure: No_service");
Break
Case SMSMANAGER.RESULT_ERROR_NULL_PDU:
LOG.I (TAG, "message received failure: NULL_PDU");
Break
Case Smsmanager.result_error_radio_off:
LOG.I (TAG, "message received failure: Radio_off");
Break
}
}
}
}

This realizes the SMS automatic reply function, certainly also can realize receives the short note automatic callback telephone. This provides a short message receive function:

Copy Code code as follows:

public void Doreceivesms (Intent Intent) {
Object[] PDUs = (object[]) Intent.getextras (). Get ("PDUs");
Smsmessage [] messages = new Smsmessage[pdus.length];
for (int i=0; i<pdus.length; i++) {
BYTE[]PDU = (byte[]) pdus[i];
Create a message from the PUD
Messages[i] = SMSMESSAGE.CREATEFROMPDU (PDU);
}
for (Smsmessage msg:messages) {
Get the contents of a text message
String content = Msg.getmessagebody ();
Get the person who sent
String sender = Msg.getoriginatingaddress ();
Time to get text messages
Long timer = Msg.gettimestampmillis ();
Converts a millisecond number to a date format
Date date = new Date (timer);
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd hh-mm-ss");
String time = Sdf.format (date);
String smscontent = time + ":" + Sender + ":" + content;
Call to send SMS method
Sendsmsmessage ("5556", smscontent);
}
}

SMS Send:

Copy Code code as follows:

public void Sendsmsmessage (string phonenumber, string content) {
Smsmanager Smsmanager = Smsmanager.getdefault ();
It's important to judge the length of a text message if the length is greater than 70.
if (Content.length () >= 70) {
list<string> list = smsmanager.dividemessage (content);
for (String mmsg:list) {
Smsmanager.sendtextmessage (PhoneNumber, NULL, mmsg, NULL, NULL);
}
}else{
Smsmanager.sendtextmessage (PhoneNumber, NULL, content, NULL, NULL);
}
}

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.