Example of operation of Android SMS receiving monitoring and automatic reply


Define Action for broadcast receivers:

private static final String TAG ="SmsService";
/**
*  Message sending status broadcast
*/
private static final String ACTION_SMS_SEND  = "com.SmsService.send"
/**
*  Message receiving status broadcast
*/
private static final String ACTION_SMS_DELIVERY = "com.SmsService.delivery";
/**
*  Message reception broadcast
*/
private static final String ACTION_SMS_RECEIVER = "android.provider.Telephony.SMS_RECEIVED";

Define and register broadcast receivers, either dynamically or statically:

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();// To obtain intent The contents of the  
   if (bundle != null) { 
    Object[] pdus = (Object[]) bundle.get("pdus");// To obtain bundle What's in there  
    for (Object obj : pdus) { 
     // The following two lines of text messages are taken and added message In the  
     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, " Text message sending failed :GENERIC_FAILURE");
     break;
    case SmsManager.RESULT_ERROR_NO_SERVICE:
     Log.i(TAG, " Text message sending failed :NO_SERVICE");
     break;
    case SmsManager.RESULT_ERROR_NULL_PDU:
     Log.i(TAG, " Text message sending failed :NULL_PDU");
     break;
    case SmsManager.RESULT_ERROR_RADIO_OFF:
     Log.i(TAG, " Text message sending failed :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 successfully ");
     break;
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
     Log.i(TAG, " Message reception failed :GENERIC_FAILURE");
     break;
    case SmsManager.RESULT_ERROR_NO_SERVICE:
     Log.i(TAG, " Message reception failed :NO_SERVICE");
     break;
    case SmsManager.RESULT_ERROR_NULL_PDU:
     Log.i(TAG, " Message reception failed :NULL_PDU");
     break;
    case SmsManager.RESULT_ERROR_RADIO_OFF:
     Log.i(TAG, " Message reception failed :RADIO_OFF");
     break;
   }
  }
 }
}

In this way, the text message automatic reply function can be realized, of course, also can realize receiving the text message automatically dial back the call. Here provides a SMS receiving function:

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];
  // from pud Created in the 1 A message
  messages[i] = SmsMessage.createFromPdu(pdu);
 }
 for(SmsMessage msg: messages){
  // Get the content of the message
  String content = msg.getMessageBody();
  // Get the person who sent it
  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 the method that sends the SMS
  sendSmsMessage("5556", smsContent);
 }
}

SMS sending:

public void sendSmsMessage(String phoneNumber, String content){
 SmsManager smsManager = SmsManager.getDefault();
 // Determine the length of the message, if the length is greater than 70 It's going to go wrong, so this is important
 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);
 }
}