1

如何获取传入短信的时间戳。这是我的代码。

 public void onReceive(Context context, Intent intent) {
    Log.i("SMS Reciever", "smsReceiver: SMS Received");
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Log.i("SMS Reciever", "smsReceiver : Reading Bundle");
        Object[] pdus = (Object[])bundle.get("pdus");
        for (int i = 0; i < pdus.length; i++) {
        SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[i]);
        SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
        String phoneNumber = currentMessage.getDisplayOriginatingAddress();
        String senderNum = phoneNumber;
        String message = currentMessage.getDisplayMessageBody();
        Log.i("SmsReceiver", "Send From: "+ senderNum + "; message: " + message);
4

2 回答 2

1

尝试从传入的短信中获取时间戳:

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
 // get Timestamp
long timstamp=currentMessage.getTimestampMillis();
于 2013-06-25T21:50:28.787 回答
0

只要您在设备上收到短信,广播接收器就会按照您的编码处理它。所以要获得接收短信的时间,您可以使用:

long smsReceiveTime = System.currentTimeMillis();

在你之后

String message = currentMessage.getDisplayMessageBody();

同样,对于日期:

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
于 2013-06-25T21:42:57.147 回答