SoFunction
Updated on 2025-04-07

Examples of Android SMS Receive and Monitoring and Automatic Reply to SMS


class SmsReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
  int resultCode = getResultCode();
  if (().equals(ACTION_SMS_RECEIVER)){
   (TAG, "SmsReceiver->onReceive");
   SmsMessage sms = null;
Bundle bundle = ();//Get the content in the intent
   if (bundle != null) { 
Object[] pdus = (Object[]) ("pdus");//Get the content in the bundle
    for (Object obj : pdus) { 
//The following two lines will take out the text message and add it to the message
     sms = ((byte[]) obj); 
    } 
   } 
   (MSG_SMS_RECEIVE,sms).sendToTarget();
//Receive information sending status
  }else if (().equals(ACTION_SMS_SEND)){
   switch (resultCode) {
    case Activity.RESULT_OK:
(TAG, "SMS sent successfully");
     break;
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
(TAG, "SMS sending failed: GENERIC_FAILURE");
     break;
    case SmsManager.RESULT_ERROR_NO_SERVICE:
(TAG, "SMS sending failed: NO_SERVICE");
     break;
    case SmsManager.RESULT_ERROR_NULL_PDU:
(TAG, "SMS sending failed: NULL_PDU");
     break;
    case SmsManager.RESULT_ERROR_RADIO_OFF:
(TAG, "SMS sending failed: RADIO_OFF");
     break;
   }
//Receive information reception status
  }else if (().equals(ACTION_SMS_DELIVERY)){
   switch (resultCode) {
    case Activity.RESULT_OK:
(TAG, "SMS received successfully");
     break;
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
(TAG, "SMS reception failed: GENERIC_FAILURE");
     break;
    case SmsManager.RESULT_ERROR_NO_SERVICE:
(TAG, "SMS reception failed: NO_SERVICE");
     break;
    case SmsManager.RESULT_ERROR_NULL_PDU:
(TAG, "SMS reception failed: NULL_PDU");
     break;
    case SmsManager.RESULT_ERROR_RADIO_OFF:
(TAG, "SMS reception failed: RADIO_OFF");
     break;
   }
  }
 }
}