来自Incoming SMS android的时间戳

时间:2013-06-25 21:23:27

标签: android

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

 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);

2 个答案:

答案 0 :(得分:1)

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

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
 // get Timestamp
long timstamp=currentMessage.getTimestampMillis();

答案 1 :(得分:0)

当您在设备上收到短信时,广播接收器会对其进行处理,因为您已对其进行了编码。因此,为了获得收到短信的时间,您可以使用:

long smsReceiveTime = System.currentTimeMillis();

之后

String message = currentMessage.getDisplayMessageBody();

以同样的方式,对于日期:

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());