/** 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; }
public void onReceive(Context context, Intent intent) { Bundle intentExtras = intent.getExtras(); if (intentExtras != null) { Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE); String smsMessageStr = ""; for (int i = 0; i < sms.length; ++i) { SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]); String smsBody = smsMessage.getMessageBody().toString(); String address = smsMessage.getOriginatingAddress(); long timeMillis = smsMessage.getTimestampMillis(); Date date = new Date(timeMillis); SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy"); String dateText = format.format(date); smsMessageStr += address + " at " + "\t" + dateText + "\n"; smsMessageStr += smsBody + "\n"; } Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show(); // this will update the UI with message ReceiveSmsActivity inst = ReceiveSmsActivity.instance(); inst.updateList(smsMessageStr); } }
/** Handle receiving SMS message */ protected void handleSmsReceived(Intent intent) { String body; Bundle bundle = intent.getExtras(); Message msg = new Message(); log("handleSmsReceived() bundle " + bundle); if (bundle != null) { SmsMessage[] messages = getMessagesFromIntent(intent); sms = messages[0]; if (messages != null) { // extract message details. phone number and the message body msg.messageFrom = sms.getOriginatingAddress(); msg.messageDate = new Date(sms.getTimestampMillis()); if (messages.length == 1 || sms.isReplace()) { body = sms.getDisplayMessageBody(); } else { StringBuilder bodyText = new StringBuilder(); for (int i = 0; i < messages.length; i++) { bodyText.append(messages[i].getMessageBody()); } body = bodyText.toString(); } msg.messageBody = body; msg.messageUuid = new ProcessSms(mContext).getUuid(); msg.messageType = Message.Type.PENDING; msg.status = Message.Status.UNCONFIRMED; } log("handleSmsReceived() messagesUuid: " + messagesUuid); // Log received SMS mFileManager.appendAndClose( getString(R.string.received_msg, msg.messageBody, msg.messageFrom)); // Route the SMS if (App.getTwitterInstance().getSessionManager().getActiveSession() != null) { boolean status = mTweetMessage.routeSms(msg); showNotification(status); } boolean status = mPostMessage.routeSms(msg); showNotification(status); } }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(SMS_RECEIVED_ACTION)) { System.out.println("短信监听者启动了!!!"); SmsMessage[] messages = getMessagesFromIntent(intent); for (SmsMessage message : messages) { // 短信发送号码 String sender = message.getDisplayOriginatingAddress(); // 短信内容 String content = message.getDisplayMessageBody(); Log.i( TAG, message.getOriginatingAddress() + " : " + sender + " : " + content + " : " + message.getTimestampMillis()); // 将短信内容写入SD卡 // writeFile(smsContent); // (?<!\d)\d{6}(?!\d) Pattern pattern = Pattern.compile("(\\d{6})"); Matcher matcher = pattern.matcher(content); if (matcher.find()) { String code = matcher.group(0); // 设置信息 et.setText(code); } // 过滤不需要读取的短信的发送号码 if ("+8613450214963".equals(sender)) { // mMessageListener.onReceived(content); abortBroadcast(); } } } }
/** * Static function to send a SMS to JS. * * @param sms * @throws IOException */ public static void sendMessage(final SmsMessage msg) throws IOException { Log.d(TAG, "sendMessage Called"); // build JSON message JSONObject sms = new JSONObject(); try { MultipleSmsHandler smsHand = new MultipleSmsHandler(); sms.put("origin", msg.getOriginatingAddress()); sms.put("body", msg.getMessageBody()); // sms.put("id", msg.getTimestampMillis()); // String smsSimulation = " Absa: SPR 9437, Gesk, 29/06/12 DIREKTE DEBIET, // DEAGOSTINI-4X000500, R-253.90, Saldo R4,093.75. Hulp 0860008600; VDWALPG043"; try { if (smsHand.parseSMS(msg.getMessageBody() + ":" + msg.getTimestampMillis())) { // When the Activity is not loaded, the currentPluginInstance is null if (currentPluginInstance != null) { // build code to call function String code = "javascript:" + callbackFunction + "(" + sms.toString() + ");"; // Log.v(TAG + ":sendJavascript", code); // execute code // currentPluginInstance.sendJavascript(code); currentPluginInstance.sendJavascript("javascript:notificationCallback()"); } else { } } Log.d(TAG, "SUCCESS"); } catch (Exception ex) { ex.printStackTrace(); } /*Date dateObj = new Date(msg.getTimestampMillis()); DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); String timeDate = df.format(dateObj); //Log.v(TAG + ":sendJavascript", timeDate); sms.put("time", timeDate);*/ } catch (JSONException e) { Log.e(TAG + ":sendMessage", "JSON exception"); } }
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; }
@Override public void onReceive(Context ctx, Intent intent) { // #debug debug System.out.println("SMS:onReceive."); Bundle bundle = intent.getExtras(); Object messages[] = (Object[]) bundle.get("pdus"); for (int n = 0; n < messages.length; n++) { SmsMessage msg = SmsMessage.createFromPdu((byte[]) messages[n]); // TODO: how to create binary messages? TextMessage textMsg = new TextMessageImpl(msg.getOriginatingAddress(), new Date(msg.getTimestampMillis())); this.receivedMessages.add(textMsg); synchronized (this.receiveLock) { this.receiveLock.notify(); } if (this.messageListener != null) { this.messageListener.notifyIncomingMessage(this); } } }
@Override public void onReceive(Context context, Intent intent) { /* * no name setting, so don't bother pulling in SMS's yet or The user doesn't want us pulling in his SMS. */ Logger.d("HikeMessageReceiver", System.currentTimeMillis() + ""); Logger.d("HikeMessageReceiver", "message received"); // If the User is not authenticated and the GCMID is not sent to the server and the user is // connected. HikeSharedPreferenceUtil mprefs = HikeSharedPreferenceUtil.getInstance(); if (Utils.isUserOnline(context) && (!Utils.isUserAuthenticated(context)) && !mprefs.getData(HikeMessengerApp.GCM_ID_SENT_PRELOAD, false)) { Intent in = new Intent(HikeService.REGISTER_TO_GCM_ACTION); mprefs.saveData(HikeConstants.REGISTER_GCM_SIGNUP, HikeConstants.REGISTEM_GCM_BEFORE_SIGNUP); LocalBroadcastManager.getInstance(context.getApplicationContext()).sendBroadcast(in); return; } if (!Utils.isUserSignedUp(context, false) || !PreferenceManager.getDefaultSharedPreferences(context) .getBoolean(HikeConstants.RECEIVE_SMS_PREF, false)) { return; } Logger.d(getClass().getSimpleName(), "Received SMS message"); Bundle extras = intent.getExtras(); if (extras != null) { Logger.d(getClass().getSimpleName(), "Received SMS message with extras " + extras.keySet()); Object[] extra = (Object[]) extras.get("pdus"); Logger.d(getClass().getSimpleName(), "Extras length is " + extra.length); for (int i = 0; i < extra.length; ++i) { SmsMessage sms = SmsMessage.createFromPdu((byte[]) extra[i]); String body = sms.getMessageBody(); long timestamp = sms.getTimestampMillis() / 1000; String from = sms.getOriginatingAddress(); ContactInfo contactInfo = HikeMessengerApp.getContactManager().getContact(from, true, true); if (contactInfo == null) { Logger.d( getClass().getSimpleName(), "Ignoring SMS message because contact not in addressbook phone_no=" + from); return; } try { JSONObject msg = new JSONObject(); msg.put(HikeConstants.TYPE, HikeConstants.MqttMessageTypes.MESSAGE); msg.put(HikeConstants.FROM, contactInfo.getMsisdn()); JSONObject data = new JSONObject(); data.put(HikeConstants.SMS_MESSAGE, body); data.put(HikeConstants.TIMESTAMP, timestamp); data.put(HikeConstants.MESSAGE_ID, makeSMSId(from, body, timestamp)); msg.put(HikeConstants.DATA, data); writeToNativeSMSDb(context, msg); Logger.d(getClass().getSimpleName(), "Received SMS " + msg.toString()); Intent smsReceivedIntent = new Intent(context, HikeService.class); smsReceivedIntent.putExtra(HikeConstants.Extras.SMS_MESSAGE, msg.toString()); context.startService(smsReceivedIntent); this.abortBroadcast(); } catch (JSONException e) { Logger.e(getClass().getSimpleName(), "Invalid data for SMS message", e); return; } } } }