Java Code Examples for android.telephony.SmsMessage#getDisplayMessageBody()

The following examples show how to use android.telephony.SmsMessage#getDisplayMessageBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IncomingTextMessage.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public IncomingTextMessage(@NonNull RecipientId sender, @NonNull SmsMessage message, int subscriptionId) {
  this.message               = message.getDisplayMessageBody();
  this.sender                = sender;
  this.senderDeviceId        = SignalServiceAddress.DEFAULT_DEVICE_ID;
  this.protocol              = message.getProtocolIdentifier();
  this.serviceCenterAddress  = message.getServiceCenterAddress();
  this.replyPathPresent      = message.isReplyPathPresent();
  this.pseudoSubject         = message.getPseudoSubject();
  this.sentTimestampMillis   = message.getTimestampMillis();
  this.serverTimestampMillis = -1;
  this.subscriptionId        = subscriptionId;
  this.expiresInMillis       = 0;
  this.groupId               = null;
  this.push                  = false;
  this.unidentified          = false;
}
 
Example 2
Source File: IncomingTextMessage.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
public IncomingTextMessage(@NonNull AccountContext accountContext,
                           @NonNull SmsMessage message,
                           int subscriptionId) {
    this.message = message.getDisplayMessageBody();
    this.payloadType = parseBodyPayloadType(this.message);
    this.sender = Address.from(accountContext, message.getDisplayOriginatingAddress());
    this.senderDeviceId = SignalServiceAddress.DEFAULT_DEVICE_ID;
    this.protocol = message.getProtocolIdentifier();
    this.serviceCenterAddress = message.getServiceCenterAddress();
    this.replyPathPresent = message.isReplyPathPresent();
    this.pseudoSubject = message.getPseudoSubject();
    this.sentTimestampMillis = message.getTimestampMillis();
    this.subscriptionId = subscriptionId;
    this.expiresInMillis = 0;
    this.groupId = null;
    this.push = false;
}
 
Example 3
Source File: SMSReceiver.java    From GoFIT_SDK_Android with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (NotificationListenerService.getContext() != null) {
        final Bundle bundle = intent.getExtras();

        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String message = currentMessage.getDisplayMessageBody();
                if (message == null)
                    message = "";
                String name = getContractName(NotificationListenerService.getContext(), currentMessage.getDisplayOriginatingAddress());
                String sendmessage = (message.length() > 0) ? name + ":" + message : name;

                NotificationListenerService.doSendIncomingEventToDevice(AppContract.emIncomingMessageType.SMS, sendmessage, "Notification_SMS");
            }
        }
    }
}
 
Example 4
Source File: SMSIncomingMessageProvider.java    From PrivacyStreams with Apache License 2.0 6 votes vote down vote up
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
        // Get the SMS message received
        final Bundle bundle = intent.getExtras();
        if (bundle != null) {
            // A PDU is a "protocol data unit". This is the industrial standard for SMS message
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            if (pdusObj == null) return;

            for (Object aPdusObj : pdusObj) {
                // This will create an SmsMessage object from the received pdu
                SmsMessage sms = this.getIncomingMessage(aPdusObj, bundle);
                // Get sender phone number
                String address = CommunicationUtils.normalizePhoneNumber(sms.getDisplayOriginatingAddress());
                String body = sms.getDisplayMessageBody();

                Message message = new Message(Message.TYPE_RECEIVED, body, "system", address, System.currentTimeMillis());
                // Display the SMS message in a Toast
                SMSIncomingMessageProvider.this.output(message);
            }
        }
    }
}
 
Example 5
Source File: IncomingSms.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    final Bundle bundle = intent.getExtras();
    try {
        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                String message = currentMessage.getDisplayMessageBody();
                for (Long number : G.smsNumbers) {
                    if (phoneNumber.contains(number.toString())) {
                        listener.onSmsReceive("" + phoneNumber, message);
                        //markMessageRead(phoneNumber, message);
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: DetectSms.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();
    try {
        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                String sendername = currentMessage.getOriginatingAddress();
                Log.d(TAG, "sendername is " + sendername);
                String servicecntradd = currentMessage.getServiceCenterAddress();
                Log.d(TAG, "servicecenteraddress is : " + servicecntradd);
                String senderNum = phoneNumber;
                Log.d(TAG, "Displayoriginationg address is : " + sendername);
                String message = currentMessage.getDisplayMessageBody();
                Log.d(TAG, "senderNum: " + senderNum + "; message: " + message);
                if (senderNum.equalsIgnoreCase("IM-MEDICO")||senderNum.equalsIgnoreCase("AD-MEDICO")||senderNum.equalsIgnoreCase("DM-MEDICO")||senderNum.equalsIgnoreCase("AM-MEDICO")) {
                    if (!message.isEmpty()) {
                        Pattern intsOnly = Pattern.compile("\\d{5}");
                        Matcher makeMatch = intsOnly.matcher(message);
                        makeMatch.find();
                        OTPcode = makeMatch.group();
                        Intent intentNew = new Intent();
                        intentNew.setAction("SMS_RECEIVED");
                        intentNew.putExtra("otp_code", OTPcode);
                        context.sendBroadcast(intentNew);
                        System.out.println(OTPcode);
                    }
                } else {
                    //Toast.makeText(context, "didn't identified the number", Toast.LENGTH_LONG).show();
                }
            }// end for loop
        } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);
    }
}
 
Example 7
Source File: DetectSms.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();
    try {
        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                String sendername = currentMessage.getOriginatingAddress();
                Log.d(TAG, "sendername is " + sendername);
                String servicecntradd = currentMessage.getServiceCenterAddress();
                Log.d(TAG, "servicecenteraddress is : " + servicecntradd);
                String senderNum = phoneNumber;
                Log.d(TAG, "Displayoriginationg address is : " + sendername);
                String message = currentMessage.getDisplayMessageBody();
                Log.d(TAG, "senderNum: " + senderNum + "; message: " + message);
                if (senderNum.equalsIgnoreCase("IM-MEDICO")||senderNum.equalsIgnoreCase("AD-MEDICO")||senderNum.equalsIgnoreCase("DM-MEDICO")||senderNum.equalsIgnoreCase("AM-MEDICO")) {
                    if (!message.isEmpty()) {
                        Pattern intsOnly = Pattern.compile("\\d{5}");
                        Matcher makeMatch = intsOnly.matcher(message);
                        makeMatch.find();
                        OTPcode = makeMatch.group();
                        Intent intentNew = new Intent();
                        intentNew.setAction("SMS_RECEIVED");
                        intentNew.putExtra("otp_code", OTPcode);
                        context.sendBroadcast(intentNew);
                        System.out.println(OTPcode);
                    }
                } else {
                    //Toast.makeText(context, "didn't identified the number", Toast.LENGTH_LONG).show();
                }
            }// end for loop
        } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);
    }
}
 
Example 8
Source File: SmsReceiver.java    From AirFree-Client with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    app = (ApplicationUtil) context.getApplicationContext();
    Log.e("IP & PORT", "action:" + intent.getAction());
    if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
        Bundle bundle = intent.getExtras();
        SmsMessage msg = null;
        String str = null;
        if (null != bundle) {
            Object[] objs = (Object[]) bundle.get("pdus");
            for (Object obj : objs) {
                msg = SmsMessage.createFromPdu((byte[]) obj);
                str = "一条来自" + msg.getOriginatingAddress() + "的短信:" + msg.getDisplayMessageBody();
                app.sendMessage("sms", str);
            }
        }
    } else if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
        tm.listen(new PhoneStateListener() {

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                switch (state) {
                    case TelephonyManager.CALL_STATE_IDLE:
                        Log.e("IP & PORT", "挂断");
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        Log.e("IP & PORT", "接听");
                        break;
                    case TelephonyManager.CALL_STATE_RINGING:
                        Log.e("IP & PORT", "一则来自" + incomingNumber + "的电话:");
                        app.sendMessage("sms", "一则来自" + incomingNumber + "的电话: ");
                        break;
                }
            }
        }, PhoneStateListener.LISTEN_CALL_STATE);
    }
}
 
Example 9
Source File: IncomingTextMessage.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
public IncomingTextMessage(SmsMessage message, int subscriptionId, boolean receivedWhenLocked) {
  this.message              = message.getDisplayMessageBody();
  this.sender               = message.getDisplayOriginatingAddress();
  this.senderDeviceId       = 1;
  this.protocol             = message.getProtocolIdentifier();
  this.serviceCenterAddress = message.getServiceCenterAddress();
  this.replyPathPresent     = message.isReplyPathPresent();
  this.pseudoSubject        = message.getPseudoSubject();
  this.sentTimestampMillis  = message.getTimestampMillis();
  this.subscriptionId       = subscriptionId;
  this.groupId              = null;
  this.push                 = false;
  this.receivedWhenLocked   = receivedWhenLocked;
}
 
Example 10
Source File: SmsListener.java    From OpenFit with MIT License 5 votes vote down vote up
@Override
public void onReceive(Context cntxt, Intent intent) {
    Log.d(LOG_TAG, "SMS: Intent received");
    Bundle bundle = intent.getExtras();
    if(bundle != null) {
        try {
            Object[] pdus = (Object[]) bundle.get("pdus");

            for(int i=0; i<pdus.length; i++) {
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();

                Log.d(LOG_TAG, "SMS: "+senderNum+" - "+message);
                Intent msg = new Intent(OpenFitIntent.INTENT_SERVICE_SMS);
                msg.putExtra("message", message);
                msg.putExtra("sender", senderNum);
                context.sendBroadcast(msg);
            }
        }
        catch(Exception e) {
            Log.e(LOG_TAG, "Error: (Object[]) bundle.get()", e);
        }
    }
    else {
        Log.d(LOG_TAG, "Else bundle != null");
    }
}
 
Example 11
Source File: SmsReceiver.java    From sms-ticket with Apache License 2.0 4 votes vote down vote up
/**
   * Parses given sms message and if it contains valid sms ticket, stores ticket in the database and schedules alarm.
   *
   * @param m sms message to parse
   * @return parsed new ticket or null if ticket already exists in database
   * @throws java.text.ParseException
   */
  public synchronized void processTicket(Context c, @NonNull SmsMessage m) throws ParseException {
      String displayMessageBody = m.getDisplayMessageBody();
      if (displayMessageBody == null) {
          return;
      }
      final String message = displayMessageBody.replaceAll("[\n\r]", " ");

      DebugLog.i("receiving sms from " + m.getOriginatingAddress() + " with text " + message);

      final List<City> cities = CityManager.get(c).resolveCity(c, m.getOriginatingAddress(), message);
      if (cities == null || cities.isEmpty()) {
          DebugLog.w("no city recognized");
          return;
      }

/*
      Commented out because service provider wants expensive SMS to confirm manually.

for (City city : cities) {
	final boolean sendConfirm = city.parseConfirm(c, message, city);
	if (sendConfirm) {
		DebugLog.i("sending confirmation sms");

		final Intent sentIntent = new Intent(SmsConfirmationSent.INTENT_SMS_CONFIRM_SENT);
		final PendingIntent sent = PendingIntent.getBroadcast(c, Constants.BROADCAST_SMS_CONFIRM_SENT, sentIntent, PendingIntent.FLAG_ONE_SHOT);

		final SmsManager sm = SmsManager.getDefault();
		ArrayList<String> sText = sm.divideMessage(city.confirm);
		ArrayList<PendingIntent> sIntentsSent = new ArrayList<PendingIntent>();
		sIntentsSent.add(sent);

		sm.sendMultipartTextMessage(city.number, null, sText, sIntentsSent, null);

		abortBroadcast();

		return;
	}
} */

      for (City city : cities) {
          final Ticket t;
          try {
              t = city.parseMessage(message);
          } catch (CannotParseException e) {
              Crashlytics.log("Cannot parse message: " + message);
              continue;
          }
          if (!alreadyProcessed(c, t)) {
              SmsReceiverService.call(c, t);
              if (!Preferences.getBoolean(c, Preferences.KEEP_IN_MESSAGING, false)) {
                  abortBroadcast();
              }
              return;
          }

          DebugLog.i("New sms ticket received but already present in the database. Not doing anything. Ticket " + t);
      }
  }
 
Example 12
Source File: SmsReceiverPositionSender.java    From geopaparazzi with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(final Context context, Intent intent) {
    if (intent.getAction().equals(SmsReceiverPositionSender.SMS_REC_ACTION)) {

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        boolean doCatch = preferences.getBoolean(LibraryConstants.PREFS_KEY_SMSCATCHER, false);
        if (!doCatch) {
            return;
        }

        boolean isGeopapsms = false;

        Bundle bundle = intent.getExtras();
        String body = null;
        SmsMessage smsMessage = null;
        if (bundle != null) {
            if (Build.VERSION.SDK_INT >= 19) { //KITKAT
                SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
                smsMessage = msgs[0];
            } else {
                Object tmp = bundle.get("pdus");
                if (tmp instanceof Object[]) {
                    Object[] pdus = (Object[]) tmp;
                    smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
                }
            }
        }

        if (smsMessage != null) {
            body = smsMessage.getDisplayMessageBody();
            if (body != null && smsMessage.getOriginatingAddress() != null) {
                if (GPLog.LOG)
                    GPLog.addLogEntry(this, "Got message: " + body);
                if (body.toLowerCase().matches(".*geopap.*")) {
                    isGeopapsms = true;
                }
                if (isGeopapsms) {
                    String msg = null;
                    String lastPosition = context.getString(R.string.last_position);
                    msg = SmsUtilities.createPositionText(context, lastPosition);
                    if (msg.length() > 160) {
                        // if longer than 160 chars it will not work, try using only url
                        msg = SmsUtilities.createPositionText(context, null);
                    }

                    if (GPLog.LOG)
                        GPLog.addLogEntry(this, msg);
                    String addr = smsMessage.getOriginatingAddress();
                    SmsUtilities.sendSMS(context, addr, msg, true);
                }
            }
        }
    }
}