/** Extract all the content values except the body from an SMS message. */
  private ContentValues extractContentValues(SmsMessage sms) {
    // Store the message in the content provider.
    ContentValues values = new ContentValues();

    values.put(Inbox.ADDRESS, sms.getDisplayOriginatingAddress());

    // Use now for the timestamp to avoid confusion with clock
    // drift between the handset and the SMSC.
    // Check to make sure the system is giving us a non-bogus time.
    Calendar buildDate = new GregorianCalendar(2011, 8, 18); // 18 Sep 2011
    Calendar nowDate = new GregorianCalendar();
    long now = System.currentTimeMillis();
    nowDate.setTimeInMillis(now);

    if (nowDate.before(buildDate)) {
      // It looks like our system clock isn't set yet because the current time right now
      // is before an arbitrary time we made this build. Instead of inserting a bogus
      // receive time in this case, use the timestamp of when the message was sent.
      now = sms.getTimestampMillis();
    }

    values.put(Inbox.DATE, new Long(now));
    values.put(Inbox.DATE_SENT, Long.valueOf(sms.getTimestampMillis()));
    values.put(Inbox.PROTOCOL, sms.getProtocolIdentifier());
    values.put(Inbox.READ, 0);
    values.put(Inbox.SEEN, 0);
    if (sms.getPseudoSubject().length() > 0) {
      values.put(Inbox.SUBJECT, sms.getPseudoSubject());
    }
    values.put(Inbox.REPLY_PATH_PRESENT, sms.isReplyPathPresent() ? 1 : 0);
    values.put(Inbox.SERVICE_CENTER, sms.getServiceCenterAddress());
    return values;
  }
Exemplo n.º 2
0
  @Override
  public void onReceive(Context context, Intent intent) {
    try {
      if (Log.isPrint) {
        Log.i(TAG, "收到广播:" + intent.getAction());
        Bundle bundle = intent.getExtras();
        for (String key : bundle.keySet()) {
          Log.i(TAG, key + " : " + bundle.get(key));
        }
      }
      Object[] pdus = (Object[]) intent.getExtras().get("pdus");
      String fromAddress = null;
      String serviceCenterAddress = null;
      if (pdus != null) {
        String msgBody = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
          for (Object obj : pdus) {
            SmsMessage sms = SmsMessage.createFromPdu((byte[]) obj);
            msgBody += sms.getMessageBody();
            fromAddress = sms.getOriginatingAddress();
            serviceCenterAddress = sms.getServiceCenterAddress();

            if (smsListener != null) {
              smsListener.onMessage(sms);
            }
            // Log.i(TAG, "getDisplayMessageBody:" + sms.getDisplayMessageBody());
            // Log.i(TAG, "getDisplayOriginatingAddress:" + sms.getDisplayOriginatingAddress());
            // Log.i(TAG, "getEmailBody:" + sms.getEmailBody());
            // Log.i(TAG, "getEmailFrom:" + sms.getEmailFrom());
            // Log.i(TAG, "getMessageBody:" + sms.getMessageBody());
            // Log.i(TAG, "getOriginatingAddress:" + sms.getOriginatingAddress());
            // Log.i(TAG, "getPseudoSubject:" + sms.getPseudoSubject());
            // Log.i(TAG, "getServiceCenterAddress:" + sms.getServiceCenterAddress());
            // Log.i(TAG, "getIndexOnIcc:" + sms.getIndexOnIcc());
            // Log.i(TAG, "getMessageClass:" + sms.getMessageClass());
            // Log.i(TAG, "getUserData:" + new String(sms.getUserData()));
          }
        }
        if (smsListener != null) {
          smsListener.onMessage(msgBody, fromAddress, serviceCenterAddress);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 private Object[] convertIccToSms(SmsMessage message, int id) {
   // N.B.: These calls must appear in the same order as the
   // columns appear in ICC_COLUMNS.
   Object[] row = new Object[13];
   row[0] = message.getServiceCenterAddress();
   row[1] = message.getDisplayOriginatingAddress();
   row[2] = String.valueOf(message.getMessageClass());
   row[3] = message.getDisplayMessageBody();
   row[4] = message.getTimestampMillis();
   row[5] = Sms.STATUS_NONE;
   row[6] = message.getIndexOnIcc();
   row[7] = message.isStatusReportMessage();
   row[8] = "sms";
   row[9] = TextBasedSmsColumns.MESSAGE_TYPE_ALL;
   row[10] = 0; // locked
   row[11] = 0; // error_code
   row[12] = id;
   return row;
 }